10

Is there a way to dynamically name variables?

What I need to do is take a list of variable names from an input file and create variables with those names. Is this possible?

Something like:

Variable <dynamic name of variable here> = new Variable(input);

Assume that I already have the Variable class taken care of, and the name of the variable is contain in a string called strLine.

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Brandon
  • 779
  • 5
  • 9
  • 28
  • 1
    Are you talking about generating code to be compiled or doing something dynamically in run-time? Please tell us your use-case and reasons for wanting to do this. – Andreas Vendel Feb 17 '11 at 19:40
  • 1
    Why would you even want to do this? How does it even matter? Even if you need to output the name of the variable somewhere, you can store the name in `Variable` class. – Yogesh Feb 17 '11 at 19:40
  • 1
    can you explain why did you post this question ? there is maybe right ways to do what you want – Steve B Feb 17 '11 at 19:40
  • The reason I want to do this is I'm creating an interpreter for a made up programming language, and I need to be able to store variables for said language. – Brandon Feb 17 '11 at 20:17

9 Answers9

15

Use a Dictionary<string, Variable>.

e.g.

var vartable = new Dictionary<string, Variable>();
vartable[strLine] = new Variable(input);
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
5

C# 4.0, using the dynamic objects:

dynamic d = new ExpandoObject();
((IDictionary<string, object>)d)["MyProperty"] =  5;
int val = d.MyProperty; // 5
xanatos
  • 109,618
  • 12
  • 197
  • 280
1

try this one,user json to serialize and deserialize:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web.Script.Serialization;

    namespace ConsoleApplication1
    {
     public class Program
     {
        static void Main(string[] args)
        {
            object newobj = new object();

            for (int i = 0; i < 10; i++)
            {
              List<int> temp = new List<int>();

              temp.Add(i);
              temp.Add(i + 1);

               newobj = newobj.AddNewField("item_" + i.ToString(), temp.ToArray());
            }

       }

    }

    public static class DynamicExtention
    {
        public static object AddNewField(this object obj, string key, object value)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();

            string data = js.Serialize(obj);

            string newPrametr = "\"" + key + "\":" + js.Serialize(value);

            if (data.Length == 2)
           {
               data = data.Insert(1, newPrametr);
            }
          else
            {
                data = data.Insert(data.Length-1, ","+newPrametr);
            }

            return js.DeserializeObject(data);
        }
     }
    }
Mo Ein
  • 47
  • 5
1

No, but you could use a Dictionary<string, Variable>, and then you can refer to each variable by its quoted name.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
1

You can't do that, but what you're looking to do is begging for a Dictionary use:

Dictionary<object, object> d = new Dictionary<string, object>();
d.Add("Variable1", value1);
d.Add("Variable2", value2);
d.Add("Variable3", value3);
Kon
  • 27,113
  • 11
  • 60
  • 86
0

Variable names should be known at compile time. If you intend to populate those names dynamically at runtime you could use a List<T>.

 var variables = List<Variable>();
 variables.Add(new Variable { Name = input1 });
 variables.Add(new Variable { Name = input2 });
 ...
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

No. You can load them into a Dictionary object, however. This allows you to still reference them (somewhat) using a name, which is a bit easier than using an Index, as you would with an Array or ArrayList.

David
  • 72,686
  • 18
  • 132
  • 173
0

I would use some sort of keyed collection, like a hashtable, dictionary, or list of structures with the name. You can then refer to the variable by name:

var value = variableDictionary["MyVariableName"];
var value = variableHashtable["MyVariableName"];
var value = variableList.First(x=>x.Name == "MyVariableName");

There is no other way to dynamically "name" a variable.

KeithS
  • 70,210
  • 21
  • 112
  • 164
-1

No. Use an array, otherwise you can't do this.

George Stocker
  • 57,289
  • 29
  • 176
  • 237
The GiG
  • 2,571
  • 2
  • 28
  • 29