473,804 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing a property using a string containing the property name?

Don
Is it possible to access the property of an object using a string containing
the name of the property you want to access? Something like:

oCustomer = New Customer
propname = "FirstName"
Console.Write(G etPropertyValue (oCustomer,prop name)) <-- returns
oCustomer.First Name

- Don
Nov 21 '05 #1
4 1250
Hi,
Check out System.Type.Inv okeMember()

"Don" <un*****@oblivi on.com> wrote in message
news:TDy9d.2786 0$a41.23499@pd7 tw2no...
Is it possible to access the property of an object using a string containing
the name of the property you want to access? Something like:

oCustomer = New Customer
propname = "FirstName"
Console.Write(G etPropertyValue (oCustomer,prop name)) <-- returns
oCustomer.First Name

- Don

Nov 21 '05 #2
Don,
In addition to System.Type.Inv okeMember, I find CallByName to be a handy
wrapper for System.Type.Inv okeMember.

http://msdn.microsoft.com/library/de...mefunction.asp

Dim value As Object = CallByName(oCus tomer, "FirstName" ,
CallType.Get)

Also I've used TypeDescriptor. GetProperties with much success.

Dim properties As System.Componen tModel.Property DescriptorColle ction
properties =
System.Componen tModel.TypeDesc riptor.GetPrope rties(oCustomer )
Dim value As Object = properties("Fir stName").GetVal ue(oCustomer)

Hope this helps
Jay
"Don" <un*****@oblivi on.com> wrote in message
news:TDy9d.2786 0$a41.23499@pd7 tw2no...
Is it possible to access the property of an object using a string
containing
the name of the property you want to access? Something like:

oCustomer = New Customer
propname = "FirstName"
Console.Write(G etPropertyValue (oCustomer,prop name)) <-- returns
oCustomer.First Name

- Don

Nov 21 '05 #3
"Don" <un*****@oblivi on.com> schrieb:
Is it possible to access the property of an object using a string containing the name of the property you want to access? Something like:

oCustomer = New Customer
propname = "FirstName"
Console.Write(G etPropertyValue (oCustomer,prop name)) <-- returns
oCustomer.First Name

Calling a method by its name
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=callbyname>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #4
Don
CallByName is, by far, the simplest of the bunch and wholly adequate for my
needs. Thanks to everyone who responded (and so quickly!)

- Don
"Don" <un*****@oblivi on.com> wrote in message
news:TDy9d.2786 0$a41.23499@pd7 tw2no...
Is it possible to access the property of an object using a string containing the name of the property you want to access? Something like:

oCustomer = New Customer
propname = "FirstName"
Console.Write(G etPropertyValue (oCustomer,prop name)) <-- returns
oCustomer.First Name

- Don

Nov 21 '05 #5

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

Similar topics

6
2359
by: Serge calderara | last post by:
Dear all, Does any one have a ready class that is able to access a configuration file and make different type of querry, read, write operation? Why re invanting the real if something already exist and as we share things in usegroup in general... thanks for your help
6
2749
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is called "form1", and I have selects called "PORTA", "PORTB" ... etc...
2
30580
by: Kumar | last post by:
Hi Folks, I have a question regarding my windows c# application. This application just reads MS Excel file and puts the data in to sql server database. In that excel file ,it has one named cell as "OutputStampTime". I am facing problem while accessing this cell value. When I open excel, I can see the value as "6:49 AM" . But when I click on that cell in the formula bar I am seeing as 01/27/2005 06:49:47 AM.
3
2797
by: Vivek Sharma | last post by:
Hi, I have created a dropdownlist as a web user control. I am using its multiple instances on the webpage. How do I access the selectedValue of each instance? All the instances have different IDs. Thanks Vivek
5
1949
by: RSH | last post by:
I havent been able to set a property from another class with out getting some sort of error. Can someone please tell me what I'm doing wrong here? Public Class Form1
4
3643
by: raj_genius | last post by:
I hav two queries, whc are as follows: FIRSTLY: is it possible to access the controls(by name) of a parent form(MDI) from its child forms??if yes then how??plzz provide a coded example in VB if possible.. for example..i hav a menu in the parent form named "Administrator" whic has an item "mnuLogIn"..now when i click on login..another child form named "frmLogIn" is displayed..what i want to happen is this: when login form(frmLogIn) is...
4
1425
by: John | last post by:
I am having a problem accessing the members in a structure via an interface and Class. ***Development Code*** Structure Person Public Name as String End Structure Interface IData Public Property Employee As Person
8
6562
by: RSH | last post by:
Hi, I have a situation where I have a Parent Object (Company) which has several public properties. The Company Object also creates an Employees object which has its ow set of functions and properties. I am trying to figure out how to access the Parent properties from the child object. Simplified example: Class Company Public ReadOnly Property Employees() As Employees
3
1578
by: Nathan Sokalski | last post by:
I have a validator that I wrote by inheriting from BaseValidator. At certain points in the code, I need to access other controls on the page containing the validator. I have the IDs of these controls, so I use the following statement to access them: Me.Page.FindControl(ControlID) However, this is returning Nothing, even though the control can be accessed in the Load event of the Page containing the control and the validator. I also...
0
9715
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10603
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
10353
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
10099
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...
0
9176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3003
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.