Thread: Naming a variable with another variable

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    27

    Naming a variable with another variable

    I'm trying to create a loop that will create some variable as it goes through and then add the variables to an array. I want to name the variables created based on their position in the array, something like this;

    Code:
    for (int i = 0; i < 5; i++)
               {
                   string name = i.ToString();
    
                   Nmode name;
    
                   wallarray[i] = name = new Nmode(Content);
    
                   name.NLoad
    
               }
    now obviosuly this isn't working since name is used more than once, but is there a work around that someone could suggest? even a way of ranbomly generateing some names may work, but I'm not sure as the program will need to be able to call these names later. I wont need to know them, but the program will.

    Thanks in advance for any help

    ES

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why can you not use another array? In the absence of a variable variables language feature (which I do not think C# has), an array or some kind of map/dictionary would be used. (Then even if variable variables are available, those other approaches tend to be better anyway.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    27
    Do you mean a second array that fills up with numbers as the loop progresses and then uses those numbers to name the variables? Like this

    Code:
                string[] arraynames = new string[2]; //hard coded length for now
    
               for (int i = 0; i < 5; i++)
               {
    
                   arraynames[0] = i.ToString();
    
                   string name = i.ToString();
    
                   Nmode = arraynames[0];
    
                   wallarray[i] = arraynames[0] = new Nmode(Content);
    
                   arraynames[0].Load();
    
               }

    that does sound like it would work better, but I'm unsure as to how I would use the contents of the second array to name the Nmodes.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    41
    Use a Dictionary<Tkey, TValue>. Use the name as the key and whatever class you are creating as the value.

    It is possible to create dynamic names by using the Emit class or the Microsoft.Csharp classes, but that's more work than you probably want to do.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    And I would also argue that just b/c it is possible does not mean it is recommended,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 04-16-2011, 12:28 PM
  2. Replies: 8
    Last Post: 02-14-2010, 04:14 PM
  3. Dynamic Variable Naming
    By scott_ill in forum C# Programming
    Replies: 5
    Last Post: 07-29-2009, 01:46 PM
  4. Variable naming
    By gin in forum C++ Programming
    Replies: 5
    Last Post: 07-12-2008, 04:15 PM
  5. Naming variable types
    By PHP in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2002, 06:52 PM