If I create user setting variables called var1 and var2, is it possible to put the value of var2 inside of var1?
For exemple:
Properties.Settings.Default.var2 = "stackoverflow";
Properties.Settings.Default.var1; // //path/stackoverflow/morepath
Properties.Settings.Default.var2 = "test";
Properties.Settings.Default.var1; // //path/test/morepath
The short answer to your question is no.
A string is just a string and there is no piece of built-in functionality that will dynamically replace %var2% in your first variable with the actual value of the second or any other variable.
If you want to somehow change the values of the variables, you will have to write some code that does this for you.
Settings is essentially just an XML file, so there is no way you can dynamically inject a variable value into an entry.
However, you can achieve what you want at runtime by dynamically updating var1 based on var2's value using the Properties.Settings.Default.Properties object.
Here is an article that has some examples of dynamically updating entries:
https://codedocu.com/Net-Framework/WPF/Basics/Settings/WPF_colon_-Create,-write-and-read-settings-dynamically?2045
Properties.Settings.Default.Properties["var2"].DefaultValue = "your updated value goes here"
Related
I want to write a key and multiple values in app.config, so that depending on the value a method is run.
for example:
let the key be "syncMode" and values to be kept are "syncAll"/"syncYest"
If the value is set as syncAll, complete data sync should happen in the main program and if syncYest is set, then only yesterday's data should be sync.
How can i write this in App.config?
Based on your requirement this looks like a simple case of single name and single value.If you want "Complete data sync in the main program" set value of key syncMode = "syncAll".Otherwise if you want "yesterdays data to sync" use syncMode = "syncYest"
You can create a custom configuration section for this. There is already a SO Post related to this. Additionally, you can also read the official MSDN page.
I have a for each loop as follows:
foreach (PCparts parts in items)
{
MessageBox.Show(parts.PartName);
}
The loop returns 4 values in the following order: MOBO, GFX, CPU, and RAM
Now my question is, is there a way to store just one particular value into a variable or even just display it somewhere else like in a label or whatever? For example, store GFX into a variable that can be used later.
If you haven't noticed yet, I classify myself as a newbie so please don't be too harsh. I am trying to learn.
Since you have items defined as List<PCParts>, you can access the objects in the list anytime you want, so long as items is in scope.
So, for example, if you had a label (call it lblName for sake of the example), then you could do this:
lblName.Text = items[1].PartName;
Where items[1] is the second PCParts in the list (GFX).
Essentially, sine you have the list, you already have the data stored and can retrieve it. You will need to know which item your looking for, if you're looking for a specific one. For example, to build on your for each loop:
for each (PCpart part in items)
{
if (part.PartName == "GFX")
{
lblName.Text = part.PartName;
}
}
You could also use similar logic to store a selected value in a variable for further use:
string selectedPartName = items[1].PartName;
Without knowing more about what you are trying to do, it's hard to give a more definitive answer.
In the interest of giving a different, yet equally useful answer to Tim, PCParts would be more amenable to this kind of operation if it were a Dictionary rather than a List. You'd be able to access the value corresponding to the "GFX" key with an expression like items["GFX"].
I have a class Cust_Result that accepts an integer parameter.
So on my main page when it loads I bind a formview to display the data that I retrieved. Now I would like to extract the value of my "id" label and assign it to a variable which I can than pass to my Cust_Result class but I keep receiving this error
"Unable to cast object of type 'System.Web.UI.WebControls.Label' to
type 'System.IConvertible'."
I am assuming it is because I am trying to send a string value to a parameter that is wanting an integer value but I am unsure on how to do the conversion.
My code
int cust;
cust = (Convert.ToInt32(FormView1.Row.FindControl("ID")));
You need a string to convert and that's the Label.Text property (not just the Label).
I'll split it into 2 steps:
Label lbl = FormView1.Row.FindControl("ID") as Label;
// option to bail out when lbl == null
cust = Convert.ToInt32(lbl.Text);
First convert the control to a Label
var label = (Label)FormView1.Row.FindControl("ID");
Then you can get the value in the label:
var cust = int.Parse(label.Text);
Look at the compiler error closely - it doesn't say anything about string and int - it's talking about IConvertible and Label - although the fact that it mentions Label instead of Control suggests that's not the code you've actually posted. Convert.ToInt32 doesn't konw what to do with a Control or a Label - in this case, I believe you want the text of the label, so I'd write this:
Label label = (Label) FormView1.Row.FindControl("ID");
// Potentially check for "label" being null here, i.e. the control wasn't found
int cust = Convert.ToInt32(label.Text);
It's not entirely clear where this value came from, but you may want to consider using int.TryParse instead of Convert.ToInt32, too.
I'd also note that Cust_Result is an unconventional name - try to:
Avoid abbreviations (Cust means Customer, I assume?)
Don't use underscores
Try to give more meaningful names - even CustomerResult doesn't really explain what it's the result of.
I am reading the contents of a list into a number of string variables so as to be able to build the content an HTML email.
The method I am using (and works for single line text boxes) is:
string Manager = item["Manager"].ToString();
However, when I try the same method for a multi-line text box I get the error:
Object reference not set to an instance of an object.
Can someone advise the best way in which to get the content of a multi-line text box into a variable that I can then reference for the other functionality mentioned?
The exception indicates that your item["Manager"] indexer does not find a column by that type. When it then tries to call .ToString() on it (null) it will result in the exception you are seeing.
Make sure you have the correct name (internal name, not display name!) for your notes column.
Or perhaps your instance of SPListItem the item variable is null.
When you try to get a OOB SharePoint Field it's best practice to use the SPBuiltInFieldId class or the ID of the Field instead of the internal name.
item[SPBuiltInFieldId.Title]
So I have 70 "nodes" which are all textboxes in WPF and I'm trying to change the value in the textbox from a function call.
I have a function called:
private void changeNode(int row, int column, int cost)
{
int nodeNumber= row * 10 + column;
call node"nodeNumber".Text = Convert.String(cost);
//example node0.Text = Convert.String(cost);
}
I determine what node I want to change then call nodeX.Text to change it however I want X to be a variable that I can rather than having to create 70 cases where I call the appropriate textbox.
I saw a couple of ways of doing this with reflection however it seemed to only work if the function had no parameters and also was within the function not a textbox in XAML.
Let me know if there is a simple way to convert say a string "node37" to call node37.Text = cost or something like that.
Sounds like your approach is wrong. Why do you have a set of strings which represent the names of the textboxes? You should instead have in-memory references to TextBox objects. If you have more than one, and you don't know how many there will be, then use an array of TextBox objects instead. You can index into the array with the number that represents the textbox you're looking to interact with.
Avoid the use of reflection, it is completely unnecessary here.
I assume you have put names for all your textboxes (you can do this dynamically if you haven't). Then you can use the answers for this question to find the appropriate control by name.
Are all your textboxes children of the same canvas or other control? Loop through the children and add the controls to a dictionary. Parse the name to get the number and use that as the key.
It is always better to use List when you are dealing with Data. Create an ObservableCollection with the DataObjects which you want to load, and now deal with the Data object rather than actual Controls.
In WPF, if you follow the rules, you should not point to the actual object. Check the sample application here :
http://www.abhisheksur.com/2010/08/woring-with-icollectionviewsource-in.html
I think you will get the approach.