Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String as variable name

Tags:

c#

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

like image 776
hradecek Avatar asked Feb 07 '12 20:02

hradecek


1 Answers

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"]
like image 200
Denis Biondic Avatar answered Nov 15 '22 17:11

Denis Biondic