Variable variables in C#

Started by
15 comments, last by Washu 18 years, 4 months ago
If you're planning to use different variables types (like your AS snippet suggests), I'd recommend using one of (specialized) collections types.
If you only need a fixed list of strings or integers, an array would be a lighter solution.
Advertisement
You could define a new native types layer utilizing the map feature so that it works identical to actionscript.

For example you could declare a intX or floatX type and invoke GetVarX( "var_name") or however you define it. You'd of course need to make sure to overload all the arithmetic operators so that they don't lose any flexibility over the real native types.
I think he's trying to get at variables that are just in a specific scope, not elements of some sort of dictionary or map. If that's the case, then he needs to use reflection. I posted code on how to do what he wants to do which will work to get at fields and properties of an object. I don't know how to just get at a variable that isn't part of an object:

using System.Reflection;using System;class dummy{    public int field;    public int Property    {        get { return 10; };    }}void f(){    int someInt = 10;    dummy c = new dummy();    Type dummyType = c.GetType();    int fieldValue = dummyType.GetField("fi" + "eld").GetValue(c);    int propertyValue = propertyType.GetProperty("Prop" + "erty").GetValue(c);    // now how do I get the value of "someInt" by name? It's not a    // property or field, so I can't do a "GetType" on anything.    Console.WriteLine("Value of field: {0}", fieldValue);    Console.WriteLine("Value of Property: {0}", propertyValue);}
Quote:Original post by smr
I think he's trying to get at variables that are just in a specific scope, not elements of some sort of dictionary or map. If that's the case, then he needs to use reflection. I posted code on how to do what he wants to do which will work to get at fields and properties of an object. I don't know how to just get at a variable that isn't part of an object:

The simple answer is: You don't. You could, of course, build a preprocessor that would build an internal registry of variables that were added and removed from said registry as they came into scope and left scope, however such a process would be rather...stupid.

He could also build a varadic type with a centralized registry that on construction registered the variable and then he could fetch it from that registry (which is essencially what you would do with a dictionary), which is all _root really is (albeight with a bit more stuff in it).

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Quote:Original post by Washu
He could also build a varadic type with a centralized registry that on construction registered the variable and then he could fetch it from that registry (which is essencially what you would do with a dictionary), which is all _root really is (albeight with a bit more stuff in it).


But wouldn't GetField and GetProperty accomplish the same thing? Since you have an actual object with a type, you can get at its fields with the GetX methods. Is there a concept in .NET like pythons "__locals__"?

Quote:Original post by _Danneman_
Does anything like that exist in C#?

Not exactly. But there are four ways to approximate this:

1.) Use the System.Reflection methods. For instance, to get the value of a field in a class, use the following.
using System;using System.Reflection;using System.Security;public class Dummy{  public string s = "stored string value";  public string X  {    get    {      return this.s;    }    set    {      this.s = value;    }  }}// Elsewhere in codepublic static void Main(){  // Use a try-catch block in real software to catch  // SecurityExceptions that might arise.  Dummy d1 = new Dummy();  Dummy d2 = new Dummy();  d2.X = "foo";  Type t = Type.GetType("Dummy");  FieldInfo fi = t.GetField("s"); // <-- equivalent variadic specification  // prints "stored string value"  Console.WriteLine("The value of the d1 field is: {0}", fi.GetValue(d1));  // prints "foo"  Console.WriteLine("The value of the d2 field is: {0}", fi.GetValue(d2));}


2.) Use a GenericDictionary to store name-value pairs.

3.) Use an array, if the values are all the same type.

4.) Wait for C# 3.0. Then you can use (3) even if the values aren't all the same type, and the compiler will infer their type from the way in which the value is used.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
Quote:Original post by smr
But wouldn't GetField and GetProperty accomplish the same thing? Since you have an actual object with a type, you can get at its fields with the GetX methods. Is there a concept in .NET like pythons "__locals__"?


No, because the instances wouldn't need to be class members. That particular solution is ugly though.
string name = "v";int index = 1;...Var v1(name + index);v = something;...Var var = Var.Get(name + index);


Disguised globals...silly idea.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This topic is closed to new replies.

Advertisement