1

Since I couldn't explain very good in my last question and I didn't get an answer that could satisfy me, I decided to open a new one.
Straight to the point, what I'm basically trying to do is compiling a variable (the value it holds) as a part of code (and specificly in my case referencing another variable)
Say I have:

int var_1, var_2, var_3 ... var_10;
for (int i = 1; i <= 10; i++)
{
    var_%i%=20; //if i is 1, then var_1's value will be set to 20, if i is 2, then var_2's value will be set to 20. So basically this loop sets the value of var_1 .. var_10 to 20
}

I can explain in an even simpler way, if in any case the latter is not clear.

int var_5;
int SomeOtherVar = 5;
var_%SomeOtherVar% = 10; // so var_5 (where 5 is the value of SomeOtherVar) is set to 10

Is this doable and if it is, what's the approach?

3
  • No, it's not doable, next. Commented Dec 30, 2012 at 22:32
  • I had exactly the same idea when I was new to programming. It might seem like a ridiculous question to a pro but I can relate to this. +1 Commented Dec 30, 2012 at 22:33
  • Well, I can't blame people for thinking about this. It's not an illogical conclusion when you only know about the existence of scalar variables. Commented Dec 30, 2012 at 22:34

4 Answers 4

4

No you can't do that, why dont you use an array?

    int[]  array = new int[3];

    for (int i = 0; i < array.Length; ++i)
    {
        array[i] = 20;
    }

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

0

It's not doable. Use an array instead. The type is int[] but I suggest you go read a tutorial about arrays to understand how to create and use them.

Comments

0

I can't think of a situation where you'd need to do this. If you wish to store values against a consecutive list of numbers, use an array. Otherwise you could use a Dictionary. For example to store "var1" = 20, "var2" = 20 as in your question, you could do this:

Dictionary<string, int> dict = new Dictionary<string, int>();

for (int i = 1; i <= 10; i++)
{
    dict.Add("var" + i.ToString(), 20);
}

Some examples of usage:

dict["var1"]              // 20
dict["var2"]              // 20
dict.ContainsKey("var3")  // true
dict.ContainsKey("var99") // false

Note: I'm concatenating the string "var" with the int from the loop just to demonstrate that you can use arbitary strings as keys to store / lookup the values. In this case it's a bit of a strange thing to do, and you'd probably be best sticking to a normal array, but my example shows how a dictionary could work with more complex keys.

Comments

0

If you want to bypass static type checking and you feel like creating some coding horror, you can use ExpandoObject combined with the dynamic keyword. Won't let you set variables in your scope, but will technically let you declare your own ones. Note that in my example I cast it to IDictionary<string, object> because I create its members' names at runtime from a string. What the following method does is create twenty members and assign their values from 0 to 19.

static dynamic SetVariables(IEnumerable<int> range)
{
    const string variableName = "var_";
    var expandoDictionary = new ExpandoObject() as IDictionary<string, object>;
    foreach (var i in range)
        expandoDictionary[variableName + i] = i;
    return expandoDictionary;
}

You can then access the members easily this way:

var container = SetVariables(Enumerable.Range(0, 20));
var value13 = container.var_13;

Please note that I do not recommend this usage, and I'd stay away from dynamic as much as I can. However, for the sake of problem solving, this can be seen as one unsafe but partial solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.