how to find and object using a variable that conatins the name of the object???

  • Thread starter Thread starter Lucas Sain
  • Start date Start date
L

Lucas Sain

Hi,

I think thta for this I have to use reflection... but I'm not shure.
How can I find/get an object at runtime by looking for its name that is
stored in a variable.
For example:
I have a DataTable called dtPersons that loads all the data of the
persons.
I have a string variable called tableVariable that stores tha name
"dtPersons".

At runtime I want to use this variable to gain access to the DataTable
how can I do this?
How do I create an instace of dtPersons using the string stored in the
variable

regards
Lucas
 
Lucas Sain said:
I think thta for this I have to use reflection... but I'm not shure.
How can I find/get an object at runtime by looking for its name that is
stored in a variable.
For example:
I have a DataTable called dtPersons that loads all the data of the
persons.

The first thing to work out is what exactly you mean by "a DataTable
called dtPersons". Do you mean you've got a variable *somewhere* called
dtPersons that holds a reference to the DataTable? If so, do you know
where exactly that variable is (ie which type it's in, and if it's an
instance variable, which instance of the type)? If so, you can do it
with reflection, certainly - but I'm not sure it's a good idea,
necessarily. It would generally be better just to put the references in
a Hashtable by name.
How do I create an instace of dtPersons using the string stored in the
variable

Create an instance of dtPersons? That would suggest that dtPersons
itself is a type, which means you'd use GetType.

If you can give us more information about exactly what you mean,
there's probably a way to do what you want.
 
Hi Jon,
First of all thanks for reply,

What I mean with "a DataTable called dtPersons" is excactly what you
descirbed: I have a variable *somewhere* called dtPersons that holds a
reference to the DataTable.

I want to assign (for example) the DataSource of a ComboBox without
knowing what the DataTable is. The name of the DataTable is stored in a
Variable "tableVariable". So then, knowing that I have the name of the
object strored in a variable and the object DOES exist how can I assign this
to the datasource of the comboBox

comboBoxPerson.DataSource = tableVariable

This of course doesn't work because tableVariable is the variable that
holds the name of the object that will be used (in this case
dtPerson)....how do I get to dtPersons

Thanks ahead
Luacs
 
Lucas Sain said:
First of all thanks for reply,

What I mean with "a DataTable called dtPersons" is excactly what you
descirbed: I have a variable *somewhere* called dtPersons that holds a
reference to the DataTable.

I want to assign (for example) the DataSource of a ComboBox without
knowing what the DataTable is. The name of the DataTable is stored in a
Variable "tableVariable". So then, knowing that I have the name of the
object strored in a variable and the object DOES exist how can I assign this
to the datasource of the comboBox

comboBoxPerson.DataSource = tableVariable

This of course doesn't work because tableVariable is the variable that
holds the name of the object that will be used (in this case
dtPerson)....how do I get to dtPersons

Right. I would suggest then that instead of making it the actual
variable name, you'd have a Hashtable mapping from name to DataTable.
You'd then just say:

comboBoxPerson.DataSource = dataTables[tableName];

If this isn't good enough, you'd use reflection:

Type t = target.GetType();
FieldInfo f = t.GetField (tableName);
DataTable dt = (DataTable) f.GetValue(target);
comboBoxPerson.DataSource = dt;

with lots of checks to make sure that the field exists, etc - look up
the documentation for each of those calls to make sure you understand
them.

The map would be a better solution though, IMO, if it does what you
need it to.
 
Jon,

Thanks look great. I haven't used hashtables but will take a loook at
it. Thanks again!!!

Regards
Lucas

Jon Skeet said:
Lucas Sain said:
First of all thanks for reply,

What I mean with "a DataTable called dtPersons" is excactly what you
descirbed: I have a variable *somewhere* called dtPersons that holds a
reference to the DataTable.

I want to assign (for example) the DataSource of a ComboBox without
knowing what the DataTable is. The name of the DataTable is stored in a
Variable "tableVariable". So then, knowing that I have the name of the
object strored in a variable and the object DOES exist how can I assign this
to the datasource of the comboBox

comboBoxPerson.DataSource = tableVariable

This of course doesn't work because tableVariable is the variable that
holds the name of the object that will be used (in this case
dtPerson)....how do I get to dtPersons

Right. I would suggest then that instead of making it the actual
variable name, you'd have a Hashtable mapping from name to DataTable.
You'd then just say:

comboBoxPerson.DataSource = dataTables[tableName];

If this isn't good enough, you'd use reflection:

Type t = target.GetType();
FieldInfo f = t.GetField (tableName);
DataTable dt = (DataTable) f.GetValue(target);
comboBoxPerson.DataSource = dt;

with lots of checks to make sure that the field exists, etc - look up
the documentation for each of those calls to make sure you understand
them.

The map would be a better solution though, IMO, if it does what you
need it to.
 
Back
Top