473,748 Members | 4,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1649
Lucas Sain <ls***@lidersof t.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.co m>
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)....ho w do I get to dtPersons

Thanks ahead
Luacs
"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** @news.microsoft .com...
Lucas Sain <ls***@lidersof t.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.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #3
Lucas Sain <ls***@lidersof t.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)....ho w 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(targ et);
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.co m>
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.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Lucas Sain <ls***@lidersof t.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)....ho w 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(targ et);
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.co m>
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
2123
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 to a variable called ShowContact ie. ShowContact = rstemp("Contact") But when the variable ShowContact is displayed.
2
5046
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 folder structure and naming conventions and then send a Net send message to those users telling them to rectify. The information I want to get is when you select the file/folder and then: Properties -> Security Tab -> Advanced Button -> Owner Tab ->...
17
3026
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). I have a dataset as follows (1 week to keep it short): Employee 1 - Date 1 Employee 1 - Date 2
7
1499
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 defined below: Public Class Variable Inherits MarshalByRefObject '<remarks/> Public strVariableName As String
5
8275
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
5018
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
2311
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 System.CodeDom.CodeCastExpression) __problem typecast #1 Desc:i do needed checks but data/commands in XML is dynamic and i don't wanna fix C# code again and again... Sample:foreach (object some in somearray) (some.GetType())some.someaction();
14
2401
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 the object, calls an editor dialog to let the user edit the object, and then - depending on the DialogResult - assigns either the clone/backup copy or the modified object itself to the reference. Maybe I'm thinking too much in pointer references...
3
7886
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. Exception Details: System.Xml.Xsl.XsltException: Cannot find the script or external object that implements prefix 'counter'. Source Error: An unhandled exception was generated during the execution of the current web request. Information...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9243
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.