0

I have a number of variables such as

int foo1;
int foo2;
int foo3;
int foo4;

Now, I have a for loop, going from var x = 0 to 3 (for the 4 variables), and i would like to use x to call the variables like this:

for(int x = 0; x < 4; x++)
{
    foo+x = bar;
}

so that when x = 1, then my variable foo1 will be assigned the value bar (foo+x = bar == foo1 = bar when x = 1).

Is there any way of doing this in C# or should I take an alternative approach?

1
  • 1
    There's a long-winded way to do it involving reflection, but you should use arrays (int[]) or a List<int> instead. Commented Aug 7, 2012 at 2:48

5 Answers 5

4

This is not possible, unless you wanted to use Reflection, which would not be the best approach. without knowing what you are trying to achieve, it is a little difficult to answer, but you could create an array to hold your variables and then use x as an indexer to access them

for(int x = 0; x < 4; x++)
{
    fooarr[x] = bar;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for all the quick answers everyone, I was just checking to see if this was possible, I'll just use a multidimensional array instead like TimC suggested (seeing that each variable is actually an array not just an int, sorry about that). Thanks again for the quick assistance
@user1275067. Don't forget that ints are value types. If you assign one of the arrays values with a value type variable, changes to the arrays values wont affect the original variable.
1

Could you do something like this:

var listVariables = new Dictionary<string, int>
                    {
                        { "foo1", 1 },
                        { "foo2", 2 },
                        { "foo3", 3 },
                        { "foo4", 4 },
                    };

for (int x = 1; x <= 4; x++)
{
   listVariables["foo" + x] = bar;
}

1 Comment

This actually is a good idea, I forgot about dictionaries, and is if fact a solution directly to what I was asking, thanks a lot :)
1

It's hard to judge what would be the best approach in your particular case, but it's most likely not the good approach. Do you absolutely need 4 VARIABLES, or only 4 values. A simple list, array, or dictionary would do the job:

int[] array = new int[4];
List<int> list = new List<int>(4);
List<int, int> dictionary1 = new Dictionary<int, int>(4);
List<string, int> dictionary2 = new Dictionary<string, int>(4);

for(int x = 0; x < 4; x++)
{
    array[x] = bar;
    list[x] = bar;
    dictionary1.Add(x, bar);
    dictionary2.Add("foo" + x.ToString(), bar);
}

Comments

0

Perhaps an alternative approach would be better ;-)

int[] foo;

// create foo

for(int i = 0; i < 4; i++)
{
  foo[i] = value;
}

Comments

0

If at all possible, you should use an array that contains four integers. You can declare it like so:

int[] foos = new int[4];

Then, in your loop, you should change to the following:

for(int i=0;i<foos.Length;i++)
{
     // Sets the ith foo to bar. Note that array indexes start at 0!
     foos[i] = bar;
}

By doing this, you will still have four integers; you just access them with foos[n], where n is the nth variable you want. Keep in mind that the first element of an array is at 0, so to get the 1st variable, you'll call foos[0], and to access the 4th foo, you'll call foos[3]

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.