-1

I have a large amount of nested foreach loops and I'm running out of variable names to use... Could I do something like this :

 //int i = 1
 string "number"+i = new String("FirstElement");

I've seen some posts about this for languages such as C and javascript, but I don't know how to do it in C#.

In case you can't name variables after other variables : how could this problem otherwise be solved?

9
  • 5
    Maybe use array, list or other collection? So you can access them like number[1] instead of number1 Commented Aug 20, 2013 at 10:35
  • and how could I implement that option into a foreach variable declaration? (eg foreach([VAR] in ~~~)? Commented Aug 20, 2013 at 10:37
  • @user2698666 could you try that: string "number" + i.ToString() = new String("FisrtELement") Commented Aug 20, 2013 at 10:39
  • Nested foreaches: if you can't handle the variables, you have a big problem. Commented Aug 20, 2013 at 10:39
  • string "number" + i.toString() = new String("FisrtELement") doesn't work Commented Aug 20, 2013 at 10:40

4 Answers 4

4

This problem could otherwise be solved by using meaningful variable names - running out of names would mean running out of concepts for what the thing is trying to achieve, which is hard to believe.

If your requirement is to work with a collection of variables, then put them in a collection; if it's to have n number of variables all strictly defined by name, then name them explicitly, and so on.

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

Comments

2
var a = new List<int>(); 
for(int i=0;i<100;i++) 
{    
     a.Add(i); 
}
Console.WriteLine(a[0]);
Console.WriteLine(a[1]);
etc....

This way you make list which contains 100 elements, you can access them by nameoflist[index]

3 Comments

and how could I implement that option into a foreach variable declaration? (eg foreach([VAR] in ~~~)?
@user2698666 foreach(var something in yourmagiclist) { a.Add(something.SomeProperty); } of course a must be a list of same type as SomeProperty
You do something like (foreach String str in listOfStrings){myMethod(str);}etc
1

A while back I asked a similar question:

Python - neat way of creating multiple objects?

Basically, if you have lots of very similar variables named things like variable_1, variable_2, etc, then thats normally a sign that you should be using an array or list of variables instead.

So instead of 10 separate string variables, create a list (or array) of strings.

Comments

0

The way you are doing will not work in C#. You can not create variables at run time. If you want you have to use a list to store your variable name and value. Therefore you better create a class to hold name of the variable and value.

class Variable
{
   string Name;
   string  Value;
}

Now create another class to hold variables

class VaraibleHolder
{
    List<Variable> Variables;
    VariableHolder()
    {
        Variables = new List<Variables>(); 
    } 

    public void Add(Variable Vbl)
    {
        Variable vbl = Variables.SingleOrDefault(v=>v.Name == Vbl.Name);
        if(vbl == null)
        {
            Variables.Add(vbl);  
        } 
    }

    public void Remove(string VblName)
    {
        //this is a lamda expression.
        Variable vbl = Variables.SingleOrDefault(v=>v.Name == VblName);
        if(vbl != null)
        {
            Variables.Remove(vbl);  
        }    
    }

    public Variable GetVariable(string VblName)
    {
         Variable vbl = Variables.SingleOrDefault(v=>v.Name == VblName);
         return vbl;  
    } 
}

So you can use Variable holder to create and store varaibles at runtime. Hope this will help you.

1 Comment

well it would be easier to use directly a Dictionary<string,string>

Your Answer

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