locked
Declaring multiple variables in a single statement? RRS feed

  • Question

  • Hello,
    Is it possible to declare multiple variables in a single statement? I was toying with a for loop:

     for (int i = 0; i < 10; i++)
                {
                    int ("Variable" + i.ToString()) = i;
                }
    
    which is not an allowed syntax. In this case all the result would be:
    Variable0 = 0;
    Variable1 = 1;
    .....
    Variable9=9;

    I want to apply this idea to more complex code, and I'll be able to figure out how to do it if I can get through this simple example.
    Thanks

    Note: In my application the number of variables that will need to be declared depends on an earlier input, so I want to be able to declare variables and assign a value to them in the same code section.
    Thursday, December 31, 2009 8:42 AM

Answers

  • This is what arrays are for...
    Use List<> class (from System.Collection.Generic namespace) to declare a list of integers (or whatever other class you need).
    You can either set the number of items in the constructor of the List<> class or simply add item by item using the .Add() method.

    hth,
    Lior.
    • Proposed as answer by Mike_999 Tuesday, January 5, 2010 10:36 AM
    • Marked as answer by Aland Li Thursday, January 7, 2010 2:21 AM
    Thursday, December 31, 2009 9:02 AM
  • Hi Jackson,

    I think what you need is a collection which can store integers with names. The generic class Dictionary<T> can meet your needs. This is a code snippet:
        private Dictionary<string, int> ints = new Dictionary<string, int>();
        private void Initialize()
        {
            for (int i = 0; i < 10; i++)
            {
                //Add the integer to the dictionary, 
                //"Variable" + i.ToString() is the name, 
                //i is the value
                ints["Variable" + i.ToString()] = i;
            }
        }
        //Get the int with a name parameter.
        private int GetInt(string name)
        {
            return ints[name];
        }

    You can get more about Dictionary<T> from:
    http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

    Let me know if this does not help.
    Aland Li
    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    • Marked as answer by Aland Li Thursday, January 7, 2010 2:21 AM
    Tuesday, January 5, 2010 9:04 AM

All replies

  • This is what arrays are for...
    Use List<> class (from System.Collection.Generic namespace) to declare a list of integers (or whatever other class you need).
    You can either set the number of items in the constructor of the List<> class or simply add item by item using the .Add() method.

    hth,
    Lior.
    • Proposed as answer by Mike_999 Tuesday, January 5, 2010 10:36 AM
    • Marked as answer by Aland Li Thursday, January 7, 2010 2:21 AM
    Thursday, December 31, 2009 9:02 AM
  • Hi Jackson,

    I think what you need is a collection which can store integers with names. The generic class Dictionary<T> can meet your needs. This is a code snippet:
        private Dictionary<string, int> ints = new Dictionary<string, int>();
        private void Initialize()
        {
            for (int i = 0; i < 10; i++)
            {
                //Add the integer to the dictionary, 
                //"Variable" + i.ToString() is the name, 
                //i is the value
                ints["Variable" + i.ToString()] = i;
            }
        }
        //Get the int with a name parameter.
        private int GetInt(string name)
        {
            return ints[name];
        }

    You can get more about Dictionary<T> from:
    http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

    Let me know if this does not help.
    Aland Li
    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    • Marked as answer by Aland Li Thursday, January 7, 2010 2:21 AM
    Tuesday, January 5, 2010 9:04 AM