locked
Creating Variables at Runtime RRS feed

  • Question

  • Is it possible to create variables?  I have a counter, and each time a button is clicked, I'd like to create a variable adding the value of the counter to the end of the name, so for instance:  variable1 variable2 variable3, etc.  

     

     

    Thursday, June 9, 2011 3:06 AM

Answers

  • Below is a sample for you to continue with. Notice that this is just a simple demo, the input object type should only have three possibilities: "DataSet", "DataTable", "DataAdapter".
    If you've added some to the objects in the construction procedure, to use them, convert the objects like this: pair.Value as System.Data.DataTable.
          Dictionary<String, Object> objList = new Dictionary<string,object>();
    
          Console.WriteLine("Please create some objects as you want:");
          Console.WriteLine("=======================================");
          while (true)
          {
            Console.Write(Environment.NewLine + "Please enter the object name: ");
            String objName = Console.ReadLine();
            Console.Write("And the object type: ");
            String objType = Console.ReadLine();
    
            var obj = new Object();
            if (objType == "DataSet")
              obj = new System.Data.DataSet();
            else if (objType == "DataTable")
              obj = new System.Data.DataTable();
            else if(objType=="DataAdapter")
              obj = new System.Data.SqlClient.SqlDataAdapter();
    
            if (obj != null)
              objList.Add(objName, obj);
    
            Console.Write("Go on adding a new object?(Y/N) ");
            ConsoleKeyInfo cki = Console.ReadKey();
            Console.WriteLine();
            if (cki.Key == ConsoleKey.Y)
              continue;
            else if (cki.Key == ConsoleKey.N)
              break;
          }
    
          Console.WriteLine(Environment.NewLine + "=======================================");
          foreach (KeyValuePair<String, Object> pair in objList)
          {
            Console.WriteLine("I am " + pair.Key + ",\t my type is " + pair.Value.GetType());
          }
    


    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 13, 2011 9:19 AM

All replies

  • Since the other parts of your program would have trouble figuring out if the variables existed or not - ask yourself - what am I trying to do? Is a simple data structure easier to use?

    Like can you use a Stack and push values up the stack? Or use an array[][] - it's simple and you code is way simpler - everyone could understand it and you can find the upper limit easily.

    Hope this helps....


    Thursday, June 9, 2011 3:16 AM
  • How would this help you in any way?

    You would never be able to refer to these variables by name in your code because they would be created at runtime.  If you were to imagine a scenario where clicking a button creates a variable called variable1, if you attempted to use variable1 as a name in your code you'd get a compiler error because it's undefined.  In order for it to become defined, the code has to compile.  So you have a chicken and egg problem.

    It just doesn't make any sense.

    What you may be after is a dictionary.

    You can create a dictionary which maps strings to objects - the string represents the "variable" name and the object is its value, for example:

    Dictionary<string,object> buttonClickVariables=  new Dictionary<string,object>();

    In your button click even handler, you could write this:

    buttonClickVariables["variable1"] = someValue;

    Evan

    Thursday, June 9, 2011 3:24 AM
  • Yeah, on second thought it probably wouldn't.  I figured there might be some foreach (varName & varCounter) statement, but I guess not.  

    The issue is that I need to allow the user to create an indefinite amount of an object, and the object obviously needs to have properties that can be defined and remembered throughout runtime.  A multidimensional array might work, I'll take a look at that.  

    Thursday, June 9, 2011 5:30 PM
  • Below is a sample for you to continue with. Notice that this is just a simple demo, the input object type should only have three possibilities: "DataSet", "DataTable", "DataAdapter".
    If you've added some to the objects in the construction procedure, to use them, convert the objects like this: pair.Value as System.Data.DataTable.
          Dictionary<String, Object> objList = new Dictionary<string,object>();
    
          Console.WriteLine("Please create some objects as you want:");
          Console.WriteLine("=======================================");
          while (true)
          {
            Console.Write(Environment.NewLine + "Please enter the object name: ");
            String objName = Console.ReadLine();
            Console.Write("And the object type: ");
            String objType = Console.ReadLine();
    
            var obj = new Object();
            if (objType == "DataSet")
              obj = new System.Data.DataSet();
            else if (objType == "DataTable")
              obj = new System.Data.DataTable();
            else if(objType=="DataAdapter")
              obj = new System.Data.SqlClient.SqlDataAdapter();
    
            if (obj != null)
              objList.Add(objName, obj);
    
            Console.Write("Go on adding a new object?(Y/N) ");
            ConsoleKeyInfo cki = Console.ReadKey();
            Console.WriteLine();
            if (cki.Key == ConsoleKey.Y)
              continue;
            else if (cki.Key == ConsoleKey.N)
              break;
          }
    
          Console.WriteLine(Environment.NewLine + "=======================================");
          foreach (KeyValuePair<String, Object> pair in objList)
          {
            Console.WriteLine("I am " + pair.Key + ",\t my type is " + pair.Value.GetType());
          }
    


    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 13, 2011 9:19 AM