Variable variables in C#

Started by
15 comments, last by Washu 18 years, 4 months ago
In for instance ActionScript (programs flash) there is a possibility of calling a variable by typing a part of its name, and add the rest of the name as the content of another variable. Ex:

var_1 = "Hello";
var_2 = 1;
var_3 = _root["var_" + var_2];

// var_3 gets the value from var_1, ie. "Hello"
Does anything like that exist in C#?
------------------------Why of course the people don’t want war. ... That is understood. But after all it is the leaders of the country who determine the policy, and it is always a simple matter to drag the people along, whether it is a democracy, or a fascist dictatorship, or a parliament, or a communist dictatorship ...Voice or no voice, the people can always be brought to the bidding of the leaders. That is easy. All you have to do is to tell them they are being attacked, and denounce the pacifists for lack of patriotism and exposing the country to danger. It works the same in any country. [Herman Goering]
Advertisement
You can do it through the reflection API, check out the System.Reflection namespace. I don't know if there's an easier way.
Hm, there might be an easier way, as I don't know C# well, but you can use arrays in the situation you have there. Something like:

(c++)
//create arrayarray[0] = "hello";int index = 0;myVar = array[index];


This will accomplish the same thing as you posted, but will not be fun or organized to have several variables each in an array like that.
Youre right, its probably easier to use arrays instead. But Ill check out System.Reflection too.

Thanks both for the help :)
------------------------Why of course the people don’t want war. ... That is understood. But after all it is the leaders of the country who determine the policy, and it is always a simple matter to drag the people along, whether it is a democracy, or a fascist dictatorship, or a parliament, or a communist dictatorship ...Voice or no voice, the people can always be brought to the bidding of the leaders. That is easy. All you have to do is to tell them they are being attacked, and denounce the pacifists for lack of patriotism and exposing the country to danger. It works the same in any country. [Herman Goering]
            Type t = theObject.GetType();            // field is a member variable            t.GetField("FieldName").GetValue(theObject));            // or get property, which is a property defined like            // int SomeProperty { get{ return 0; } }            t.GetProperty("PropertyName").GetValue(theObject, null));
Does ActionScript have arrays, even? o_O

If you really need to be able to map from a "name" to some "value", then use - a map.

#include <map>#include <string>//...std::map<std::string, std::string> vars;vars["1"] = "Hello";vars["2"] = "1"; // can't store an int. C++ is statically typed.vars["3"] = vars[vars["2"]];
Quote:Original post by Zahlman
*** source code removed ***

Psst. C#. Not C++.
Quote:Original post by SiCrane
Quote:Original post by Zahlman
*** source code removed ***

Psst. C#. Not C++.

I'm pretty sure the corresponding class is System.Collections.Generic.Dictionary<Key, Value>

CM
Oops. (Don't know enough C# to help, and yeah, obviously I've been helping way too many people with C++ in a row... come on people, use a more friendly language :) )
A possible way for this to work is definately an array, here is some sample code if you're still having trouble.

string[] var;
var = new string[];

var[1] = "Hello";
var[2] = "1";
var[3] = var[1] + var[2];

Yes using that code var[2] won't be and integer so if you were to add numbers in that array 1 + 2 = 12 so be careful about that. If you need more help just PM me or something.

This topic is closed to new replies.

Advertisement