473,320 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

evaluating string to a variable name

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?
}
Nov 17 '05 #1
6 12727
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.

"Raed Sawalha" <Ra*********@discussions.microsoft.com> wrote in message
news:F8**********************************@microsof t.com...
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?
}

Nov 17 '05 #2
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

"Peter Rilling" wrote:
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.

"Raed Sawalha" <Ra*********@discussions.microsoft.com> wrote in message
news:F8**********************************@microsof t.com...
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?
}


Nov 17 '05 #3
Raed Sawalha wrote:
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

Nov 17 '05 #4
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

"Raed Sawalha" <Ra*********@discussions.microsoft.com> wrote in message
news:73**********************************@microsof t.com...
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

"Peter Rilling" wrote:
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.

"Raed Sawalha" <Ra*********@discussions.microsoft.com> wrote in message
news:F8**********************************@microsof t.com...
>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?
> }
>
>


Nov 17 '05 #5
Peter Rilling wrote:
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

Nov 17 '05 #6
You are right, for some reason I was thinking in just local variable terms.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Peter Rilling wrote:
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

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Daniel Ehrenberg | last post by:
One of the most common bugs that people have on the Python Tutor list are caused by the fact the default arguments for functions are evaluated only once, not when the function is called. The...
3
by: Kevin Rollo | last post by:
I'm playing with a generic routine to export data, the concept is to have a list of data driven templates defining what fields to output. Evaluating a simple variable field name in the function...
6
by: markoniinimaki | last post by:
Hi, suppose I get a document name a.xml and a path //foo/bar (which nodes eventually to read from it) from another doc. Problem: how to combine them so that the whole expression gets...
6
by: encoad | last post by:
Hi everyone, I've run into another road block that google.com isn't able to solve since I don't even know where to start when searching. In my webapp, a certain number of table rows containing...
10
by: Bilal | last post by:
Hello, I'm trying to perform some string manipulations in my stylesheet and have gotten stuck on the issue below so hopefully can elicit some useful hints. Namely, the problem is that I need to...
3
by: Hvid Hat | last post by:
Hi I want to highlight (make it bold) a word in some text I'm getting in XML format. My plan was to replace the word with a bold (or span) tag with the word within the tag. I've found the code...
8
by: troy_lee | last post by:
I want to look at the values of six combo boxes. If any of the boxes are not null, I want to change the value of a separate text box. How can I look at all of them at once? Thanks in advance. ...
6
by: eureka2050 | last post by:
Hi, I am a PHP coder, recently ran into a bit of a problem trying to evaluate expressions in PHP. I have an expression which is stored in a string variable and when I try to evaluate it, it...
2
by: Looch | last post by:
All, I'm trying to output but I can only get (brackets for clarity) when using the code below. How can I "break" into the query variable in the InsertName method to add the name parameter to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.