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#?
Variable variables in C#
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:
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++)
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.
(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 :)
Thanks both for the help :)
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.
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 SiCraneQuote: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.
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
Popular Topics
Advertisement