hradecek
hradecek

Reputation: 2513

String as variable name

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

Answers (8)

erdemu
erdemu

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

elliott.io
elliott.io

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

Justin
Justin

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

Brandon Moore
Brandon Moore

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

chrs
chrs

Reputation: 6106

Seems like a bad idea. Try with enums or own datatypes /classes

Upvotes: 0

Marc Gravell
Marc Gravell

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

Denis Biondic
Denis Biondic

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

Oded
Oded

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

Related Questions