It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a string variable which has the value that the user taps. I need to make another variable which its name will be the string's value.
How do / can I do that?
No, you cannot do that: the closest you can get is a Dictionary<string,object> (you can replace the object with some other type). Using this dictionary you would be able to create associations between strings (known as "keys") and values stored in the dictionary.
IDictionary<string,object> variables = new Dictionary<string,object>();
string varName = "hello";
variables[varName] = "world";
Console.WriteLine("Name: {0} Value: {1}", varName, variables[varName]);
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
In my class partData the FW_Step attribute isfrom the type double?
When I try to format it like that
partData.FW_Step.Value.ToString("F3")
It's fail when the value is null
How can I use the format when the value is null?
You can't format when it's null; hopefully the reasons why are obvious. You need to check for the value first:
string formattedValue;
if (partData.FW_Step.HasValue)
formattedValue = partData.FW_Step.Value.ToString("F3");
else
formattedValue = "default value for null";
You can make this code shorter using a ternary expression:
string formattedValue = partData.FW_Step.HasValue ? partData.FW_Step.Value.ToString("F3") : "default value for null";
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am looking for something like PHP's associative arrays that supports nesting too. For instance, I am creating a Dictionary Object like following:
System.Collections.Specialized.OrderedDictionary userRoles = new System.Collections.Specialized.OrderedDictionary();
userRoles["UserId"] = "120202";
userRoles["UserName"] = "Jhon Doe";
// 2D array Like
userRoles["UserRoles"][0] = "CAN_EDIT_LIST";
userRoles["UserRoles"][1] = "CAN_EDIT_PAGE";
Then I would like to access them by KeyNames instead of index values. Is it possible?
OrderedDictionary uses objects for both keys and values.
To achieve nesting, just set the value to be another dictionary:
userRoles["UserRoles"] = new Dictionary();
Then you can use:
((Dictionary())userRoles["UserRoles"])["MyKey"] = "My Value";
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
foreach (var item in HashSet<T>)
{
}
What should be the var?
Your code doesn't compile, you should use something like:
HashSet<int> hashSet = new HashSet<int>(); //create a HashSet of integers
//populate hashSet
foreach(var value in hashSet){ //where var is of type int
//...
}
var (implicitly typed variable) will be exactly the type you have defined in T when you declared your HashSet.
So for example, if you declared it HashSet<int>, var will be of type int.
var is T (30 characters) (30 characters) (30 characters) (30 characters)
var is T, float the mouse over var and VS will tell you the type.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How having an array of objects with property "name" get array of strings (with names corresponding to each object)?
Assuming that you want to select a specific property from each object in an array. If that property is called Name and is of type string, then you can do the following:
IEnumerable<string> names = namedObjects.Select(x => x.Name);
ObjectWithNameString[] objects = ...?
string[] names = new string[objects.Length];
for (int i = 0; i < objects.Length; i++)
{
names[i] = objects[i].Name;
}
For a better answer expand and clarify the question.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a list and a column; the column might have many values, and each value's length may vary.
If the length exceeds 100, I want to append /n at the end of it.
Need your help.
I give it a try, though a lot of questions are open (what type of list, what a column, ...).
In short: you can use String.Insert to insert text at a specified postition.
Assuming you have a List<Foo>, the class Foo has a string property Value (your column). If it's Length exceeds 100 the line should be wrapped:
foreach(Foo foo in foos)
{
if(foo.Value.Length > 100)
foo.Value = foo.Value.Insert(100, Environment.NewLine);
}
http://msdn.microsoft.com/en-us/library/system.string.insert.aspx
Here's running code with sample data.
http://ideone.com/sUKLk