access a variable using a value from db column for its name.

M

mcnewsxp

I want to do this:

string ReplaceValue = InterpretAsVariable(myRow2["ReplaceControl"].ToString());

but myRow2["ReplaceControl"] actually contains then name of a variable in my program. so I want to interpret or evaluate the contains of myRow2["ReplaceControl"] into an existing program variable and access the value of the variable.

how to?

tia,
mcnewsxp
 
A

Arne Vajhøj

I want to do this:

string ReplaceValue =
InterpretAsVariable(myRow2["ReplaceControl"].ToString());

but myRow2["ReplaceControl"] actually contains then name of a
variable in my program. so I want to interpret or evaluate the
contains of myRow2["ReplaceControl"] into an existing program
variable and access the value of the variable.

how to?

If you have a string with the name of a property and you know the
class and you have an instance of that object, then you can get
the value via reflection.

If you have a string with the name of a local variable then you
can not do anything as the name is lost during compilation.

Other scenarios also exists. Please explain more about your
context.

Arne
 
M

mcnewsxp

I want to do this:
string ReplaceValue =
InterpretAsVariable(myRow2["ReplaceControl"].ToString());

but myRow2["ReplaceControl"] actually contains then name of a
variable in my program. so I want to interpret or evaluate the
contains of myRow2["ReplaceControl"] into an existing program
variable and access the value of the variable.

how to?



If you have a string with the name of a property and you know the

class and you have an instance of that object, then you can get

the value via reflection.



If you have a string with the name of a local variable then you

can not do anything as the name is lost during compilation.



Other scenarios also exists. Please explain more about your

context.



Arne

I am trying to print a small document that will get it's contents from a dbtable. the table will contain label text and will also tell the app whereto get data. for some lines of output the data will come from a control on the screen. so a db column will contain the control's name. in other cases the data will come from a program variable.
the table will be populated accordingly so the variable name will only be stored in the table where it is known that it will exist in the application.

labletext replacecontrol replacevariable
 
M

mcnewsxp

I want to do this:
string ReplaceValue =
InterpretAsVariable(myRow2["ReplaceControl"].ToString());

but myRow2["ReplaceControl"] actually contains then name of a
variable in my program. so I want to interpret or evaluate the
contains of myRow2["ReplaceControl"] into an existing program
variable and access the value of the variable.

how to?



If you have a string with the name of a property and you know the

class and you have an instance of that object, then you can get

the value via reflection.



If you have a string with the name of a local variable then you

can not do anything as the name is lost during compilation.



Other scenarios also exists. Please explain more about your

context.



Arne

I think I have it:

ReplaceValue = (string)this.GetType().GetField(myRow2["ReplaceVariable"].ToString()).GetValue(this);
 
A

Arne Vajhøj

If you have a string with the name of a property and you know the
class and you have an instance of that object, then you can get
the value via reflection.

If you have a string with the name of a local variable then you
can not do anything as the name is lost during compilation.

Other scenarios also exists. Please explain more about your
context.

I think I have it:

ReplaceValue = (string)this.GetType().GetField(myRow2["ReplaceVariable"].ToString()).GetValue(this);

That is an example of reflection. I would use properties instead of
fields but it is the same mechanism.

Arne
 
M

mcnewsxp

If you have a string with the name of a property and you know the
class and you have an instance of that object, then you can get
the value via reflection.

If you have a string with the name of a local variable then you
can not do anything as the name is lost during compilation.

Other scenarios also exists. Please explain more about your
context.
I think I have it:
ReplaceValue = (string)this.GetType().GetField(myRow2["ReplaceVariable"].ToString()).GetValue(this);



That is an example of reflection. I would use properties instead of

fields but it is the same mechanism.



Arne

it's working with fields, but i'd like to know why properties would be better.
thanks,
mike
 
A

Arne Vajhøj

I think I have it:

ReplaceValue = (string)this.GetType().GetField(myRow2["ReplaceVariable"].ToString()).GetValue(this);

That is an example of reflection. I would use properties instead of
fields but it is the same mechanism.

it's working with fields, but i'd like to know why properties would be better.

You computer will not meltdown by using fields. But I think properties
are better in this case.

It is generally accepted that the best is:
* private fields
* public properties if necessary

If you store the names of private fields in a database, then
those fields names are no longer internal to the class. They
are part of the class exposed interface.

I don't like names of private fields to be part of the class
exposed interface.

A public property is part of the class exposed interface, so
it is much less problematic to store its name.

That is the first reason.

My second reason is that at some point in time you may decide
to refactor and move those fields out to a separate class. And
then public properties will still work while private fields
will not.

Arne
 
J

Jeff Johnson

I want to do this:

string ReplaceValue =
InterpretAsVariable(myRow2["ReplaceControl"].ToString());

but myRow2["ReplaceControl"] actually contains then name of a variable in
my program.
so I want to interpret or evaluate the contains of
myRow2["ReplaceControl"] into an
existing program variable and access the value of the variable.

I understand what you're asking for, and it sounds really convoluted. You
seem to have some sort of dynamic configuration going on with the added
bonus of a level of indirection. (Your database doesn't contain an override
value, it contains the name of a variable and that variable is what holds
the override value.)

Ultimately, you're doing a lookup. Why knock yourself out doing it the
arcane way when you could just have one or two dictionaries lying around
with name/value pairs?
 
M

mcnewsxp

I want to do this:
string ReplaceValue =
InterpretAsVariable(myRow2["ReplaceControl"].ToString());

but myRow2["ReplaceControl"] actually contains then name of a variable in
my program.
so I want to interpret or evaluate the contains of
myRow2["ReplaceControl"] into an
existing program variable and access the value of the variable.



I understand what you're asking for, and it sounds really convoluted. You

seem to have some sort of dynamic configuration going on with the added

bonus of a level of indirection. (Your database doesn't contain an override

value, it contains the name of a variable and that variable is what holds

the override value.)



Ultimately, you're doing a lookup. Why knock yourself out doing it the

arcane way when you could just have one or two dictionaries lying around

with name/value pairs?

because I Googled some examples I don't understand how to use it. you are correct with what I am doing tho.
 
M

mcnewsxp

On 7/31/2013 1:12 PM, mcnewsxp wrote:
I think I have it:

ReplaceValue = (string)this.GetType().GetField(myRow2["ReplaceVariable"].ToString()).GetValue(this);

That is an example of reflection. I would use properties instead of
fields but it is the same mechanism.
it's working with fields, but i'd like to know why properties would be better.



You computer will not meltdown by using fields. But I think properties

are better in this case.



It is generally accepted that the best is:

* private fields

* public properties if necessary



If you store the names of private fields in a database, then

those fields names are no longer internal to the class. They

are part of the class exposed interface.



I don't like names of private fields to be part of the class

exposed interface.



A public property is part of the class exposed interface, so

it is much less problematic to store its name.



That is the first reason.



My second reason is that at some point in time you may decide

to refactor and move those fields out to a separate class. And

then public properties will still work while private fields

will not.
when you refer to properties do you mean create a get/set for them?
 
A

Arne Vajhøj

built in support?

In C# you do not:

private string s;
public string GetS()
{
return s;
}
public void SetS(string s)
{
this.s = s;
}

but:

private string s;
public string S
{
get { return s; }
set { s = value; }
}

or even shorter:

public string S { get; set; }

Arne
 

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