Lucas Sain <ls***@lidersoft.com> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too