evaluating string to a variable name

G

Guest

i have the following case

a MyClass class has
method with 2 arguments as
void SetProp(string varname,string varvalue);

in MyClass i have following members
string stdname,stddate,stdbirth,stdloc

from other class i created object from MyClass ,

MyClass obj = new MyClass();
obj.SetProp("stdname","George Asper");

what i need to achieve inside SetProp when I get to evaluate varname string
to the local memeber variable name, means

SetProp(string varname,string val)
{
evaluate(varname) = val;
//the string varname will be evaluated to local class variable and set
its relative value, is this possible? hope i explain what i need well?
}
 
P

Peter Rilling

You cannot do this because variables have no names when compiled. Symbols
are only useful for developers in their IDE. You could, however, use a
Hashtable which can be used to associate name-value pairs.
 
G

Guest

ok am in discussing with Macromedia Flash Developer, he claimed he can pass a
var name as string then flash can evaluate it as var in local member of class
 
M

Marc Gravell

If you are trying to access an instance-field defined against the class,
then this is fairly simple using reflection; something along the lines of:
GetType().GetField(varname).SetValue(this, val);

If the field is private, you may need to use the BindingFlags.NonPublic
binding-attribute.

Note also that you can pretty-much forget about this if you use any
obfuscation tool, since your field name (at runtime) will probably be "a",
a214, or something equally weird.

Note that this will always be slower than using fields / properties directly
(and I would advise only allowing this type of access to properties, not
fields, since your properties can do validity checking etc)

Marc
 
J

Jon Skeet [C# MVP]

Raed said:
i have the following case

a MyClass class has
method with 2 arguments as
void SetProp(string varname,string varvalue);

in MyClass i have following members
string stdname,stddate,stdbirth,stdloc

from other class i created object from MyClass ,

MyClass obj = new MyClass();
obj.SetProp("stdname","George Asper");

what i need to achieve inside SetProp when I get to evaluate varname string
to the local memeber variable name, means

SetProp(string varname,string val)
{
evaluate(varname) = val;
//the string varname will be evaluated to local class variable and set
its relative value, is this possible? hope i explain what i need well?
}

Well, this is generally not a good idea - it would be better to have
proper properties, and do:

obj.StdName = "George Asper";

However, if you really need to do it, you can use Type.GetField to get
a FieldInfo, and then set the value there. Assuming these are private
fields, you'll need to pass an appropriate BindingFlags value including
BindingFlags.NonPublic | BindingFlags.Instance.

Jon
 
J

Jon Skeet [C# MVP]

Peter said:
You cannot do this because variables have no names when compiled.

Yes they do. Local variables don't (or rather, the name is only in the
pdb file if one is generated) but instance/static variables are
available via reflection.

It's rarely a good idea to use that, admittedly...

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top