String as a variable name

Is it possible in C # to use String as a variable name?
I have:

String x = "matrix1_2"; Microsoft.VisualBasic.PowerPacks.RectangleShape y = ???; 


??? - there must be a variable name ... matrix1_2

+4
source share
7 answers

No, you cannot, and there is no reason to honestly have such a feature.

If you need to dynamically assign some data with a key and value, you can use a dictionary:

 Dictionary<string, RectangleShape> shapes = new Dictionary<string, RectangleShape>(); shapes.Add("matrix1_2", new RectangleShape( ... )); 

Then you can just read the "variable", for example

 shapes["matrix1_2"] 
+7
source

No, it is not. If "matrix1_2" is a local variable, you cannot do this, since the variable may not even exist after the compiler finishes, if it is actually an instance field, then reflection can help:

 object value = obj.GetType().GetField(fieldName).GetValue(obj); 
+2
source

It's impossible.

You do not have dynamic variable names in C #, VB.NET, or any .NET language. There is no support for this feature.

+1
source

Seems like a bad idea. Try enumerations or native data types / classes

0
source

You could with reflection, but I suspect that in this case theere is the best design you could implement, it would be better than using reflection. If you provide a little more information, perhaps one of us can help you with this.

Declare a dictionary variable:

 Dictionary<string, RectangleShape> rectangleDictionary = new Dictionary<string, RectangleShape>(); 

Then, where you would specify the normal code "matrix1_2 = somevalue;", instead write:

 rectangleDictionary.add("matrix1_2", somevalue) 

Then you can work with the variable name:

 rectangleDictionary["matrix1_2"] = someothervalue; rectangleDictionary["matrix1_2"].someproperty = something; Microsoft.VisualBasic.PowerPacks.RectangleShape y = rectangleDictionary["matrix1_2"]; 
0
source

Does matrix1_2 already exist? In other words, when you say y = ??? You are trying to access an existing variable or create one.

As far as I know, there is no way to create a new instance of "named" at run time. However, you can get 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 any matrix1_2 class.

0
source

You can get the value of a field from a dynamic type this way (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 the dynamic type generated at runtime (but not required), and "fieldName" is the string for the name of the property you are looking for.

0
source

Source: https://habr.com/ru/post/1395301/


All Articles