473,394 Members | 1,694 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,394 software developers and data experts.

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

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
Nov 15 '05 #1
4 1623
Lucas Sain <ls***@lidersoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2
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
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP***********************@news.microsoft.com. ..
Lucas Sain <ls***@lidersoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #3
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
Nov 15 '05 #4
Jon,

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

Regards
Lucas

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
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

Nov 15 '05 #5

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

Similar topics

2
by: Les Juby | last post by:
Tearing heair out here ........... and there must be a simple solution. The words Joe Peter Smith are stored in an Access-97 field called Contact and are returned in a record set and stored...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
17
by: Justin Emlay | last post by:
I'm hopping someone can help me out on a payroll project I need to implement. To start we are dealing with payroll periods. So we are dealing with an exact 10 days (Monday - Friday, 2 weeks). ...
7
by: William Apple | last post by:
Despite the fact this deals with webservices I believe it is a VB question. I am working on a test application that passes data to a webservice. The webservices takes a variable type that is...
5
by: Joerg Battermann | last post by:
Hello there, I have a custom type defined via Public Class Requirement Public IDNumber As Integer Public Name As String Public Description As String Public VersionPlanAttributes As New _
5
by: sfeher | last post by:
Hi All, I need to call a function(loaded with appendChild) for which I have the name as a string. .... var fnName = 'fn1'; var call = fnName + '('+ param +' )'; eval(call);
5
by: SunnyDrake | last post by:
HI! I wrting some program part of it is XML config parser which contains some commands(for flexibility of engenie). how do i more simple(if it possible not via System.Reflection or...
14
by: Philipp Reif | last post by:
Hi all, I've got a little hole in my head concerning references. Here's what I'm trying to do: I'm calling a function, passing the reference of a business object for editing. The function clones...
3
by: Abhinavnaresh | last post by:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.