-8

Sorry if this is a duplicate, but I didn't find what I need (over 30 minutes of searching the web)

I want to create variable with name from other variable (string) Is there a possibility to do this in c#? I'm new to c#.

maniak1982
  • 707
  • 2
  • 7
  • 23
Pawiczekk
  • 11
  • 1
  • 2
  • 8
    Why would you need to change a variable name at runtime? – ken2k Jul 20 '15 at 14:48
  • 1
    What have you tried? How are you going to use this dynamically-named variable? You might be better off using a collection of some sort. – maniak1982 Jul 20 '15 at 14:48
  • 1
    I usually use a Dictionary for that kind of thing. You'll need to explain your use case better if you expect help. – Conrad Frix Jul 20 '15 at 14:48
  • Take a look: http://stackoverflow.com/questions/20857773/create-dynamic-variable-name – Leo Chapiro Jul 20 '15 at 14:49
  • I don't need dynamicly-named variable. I just want to set it's name from other – Pawiczekk Jul 20 '15 at 14:49
  • I like the idea of a `Dictionary` for this. My wife had to do something similar in Java, so I had her use a `Hashmap`, which works similarly. – maniak1982 Jul 20 '15 at 14:50
  • 2
    @Pawiczekk You just said "I don't need X. All I need is X!". – Luaan Jul 20 '15 at 14:54
  • @Luaan I said that I want to create variable but I don't need changing it's name to other than from this variable – Pawiczekk Jul 20 '15 at 15:00
  • There are some (very old language, several xBase dialects) which allow such constructs. But we old ones used this feature as a replacement for arrays ... and tried to coop with the problems we faced from this ugly hacks. C# will hopefully NEVER allow this - and before someone yells at me: I know there is the dynamic type .... – Thomas Krojer Jul 20 '15 at 16:14

2 Answers2

5

No, you can't, and it makes no sense honestly to have a feature like that.

If you want , you can use a dictionary, where you can have the key be a string:

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

Then you can simply read the "variable" like

shapes["nameTheKey"]
Olivier Poulin
  • 1,778
  • 8
  • 15
2

No, and it doesn't make any sense.

Variable (or more precisely, local) names are only important at compile-time, and they only have values at runtime. If you add a new variable, you'd also need to add code to handle that variable - but how do you write and use that code without the compiler?

However, if you simply want to store some data in a key-value fashion, you can use a dictionary:

var data = new Dictionary<string, decimal>();

The first type argument is the key, and the second is the value. Do note that the values all have to be of the same type, or a type that derives from some common type.

If you want something more like JavaScript, you can have a look at the dynamic features of C# (dynamic, ExpandoObject, ...) - but frankly, you'll be better off adopting the idioms of C# 99% of the time.

ExpandoObject allows you to use code like this:

dynamic item = new ExpandoObject();
((IDictionary<string, object>)item)["Message"] = "Hi!";

Console.WriteLine(item.Message);

But again, it's rarely the best way to do something :)

Luaan
  • 62,244
  • 7
  • 97
  • 116