Reputation: 2513
is it possible in C# to use a String like a variable name ?
I have got a:
String x = "matrix1_2";
Microsoft.VisualBasic.PowerPacks.RectangleShape y = ???;
??? - there should be the name of variable...matrix1_2
Upvotes: 8
Views: 39147
Reputation: 1
You can use a string in Control.Find
function like below statement.
ComboBox comboBoxElement = (ComboBox)this.Controls.Find("comboBoxNameThatYouSearch", true)[0];
Upvotes: -1
Reputation: 41
You can get a field value from a dynamic type in this manner (using C# in a Silverlight 5 project).
Type itemType = item.GetType();
try
{
PropertyInfo field = itemType.GetProperty(fieldName);
object val = field.GetValue(item, null);
}
catch (Exception ex)
{
// field doesn't exist, do something else
}
*Where "item" is a dynamic type generated at runtime (but doesn't have to be) and "fieldName" is a string for the property name you are looking for.
Upvotes: 0
Reputation: 8943
Does "matrix1_2" exist already? In other words, when you say y = ??? are you trying to access an existing variable or create one.
To my knowledge, there is no way to create a new "named" instance at run-time. However, you can retrieve the names of existing fields and select them using System.Reflection.
String x = "matrix1_2";
Microsoft.VisualBasic.PowerPacks.RectangleShape y;
Type type = typeof(MyType); // Get type pointer
FieldInfo[] fields = type.GetFields();
foreach (var field in fields)
{
if (field.Name == "matrix1_2")
{
y = field;
}
}
MyType
above is the name of whatever class matrix1_2
lives in.
Upvotes: 0
Reputation: 8790
You could with reflection, but I suspect in this case theere is a better design you could implement that would be better than using reflection. If you provide a little more info probably one of us could help you with that.
Declare a dictionary variable:
Dictionary<string, RectangleShape> rectangleDictionary = new Dictionary<string, RectangleShape>();
Then, where you would normallay write "matrix1_2 = somevalue;", instead write:
rectangleDictionary.add("matrix1_2", somevalue)
Then you'll be able to work with the variable name:
rectangleDictionary["matrix1_2"] = someothervalue;
rectangleDictionary["matrix1_2"].someproperty = something;
Microsoft.VisualBasic.PowerPacks.RectangleShape y = rectangleDictionary["matrix1_2"];
Upvotes: 0
Reputation: 1064014
No it is not. If "matrix1_2" is a local variable, then you can't do it as the variable might not even exist after the compiler is through, if it is actually an instance field, then reflection may help:
object value = obj.GetType().GetField(fieldName).GetValue(obj);
Upvotes: 2
Reputation: 8201
No, you can't, and it makes no sense honestly to have a feature like that.
If you need to dynamically assign some data with key and value, you could use an dictionary:
Dictionary<string, RectangleShape> shapes = new Dictionary<string, RectangleShape>();
shapes.Add("matrix1_2", new RectangleShape( ... ));
Then you can simply read the "variable" like
shapes["matrix1_2"]
Upvotes: 15
Reputation: 499302
This is not possible.
You can't have dynamic variable names in C#, VB.NET or any .NET language. There is no support for such a feature.
Upvotes: 2