Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2006

Tip: Looking for answers? Try searching our database.

Dynamically Determining an Objects Property

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Karl Pierburg - 21 Mar 2006 23:51 GMT
I have an object with 30+ exposed properties.  How can I, based on a string
input, retrieve the value of a specific property?  

Here's what I'm doing right now:

Function GetField(ByVal FieldName as String) as String
  Select Case FieldName
      Case "Field1" Return myObj.Field1
      Case "Field2" Return myobj.Field2
 END Select
End Function

There's got to be a better way then this.  Is Reflection what I'm after?  
Tim Wilson - 22 Mar 2006 00:08 GMT
Yes, you could use reflection for that.
http://msdn2.microsoft.com/en-us/library/system.type.getproperty.aspx

Signature

Tim Wilson
.NET Compact Framework MVP

> I have an object with 30+ exposed properties.  How can I, based on a string
> input, retrieve the value of a specific property?
[quoted text clipped - 9 lines]
>
> There's got to be a better way then this.  Is Reflection what I'm after?
Karl Pierburg - 22 Mar 2006 15:38 GMT
Ok...I think I see where this is going, but I'm still having some issues.

I can do the following:

myObj.GetType.GetProperty("FirstName")

This Returns a PropertyInfo object.  But I can't seem to setup the
"GetValue" method to return the actual string.

Where am I going wrong?

KP

> Yes, you could use reflection for that.
> http://msdn2.microsoft.com/en-us/library/system.type.getproperty.aspx
[quoted text clipped - 13 lines]
> >
> > There's got to be a better way then this.  Is Reflection what I'm after?
Tim Wilson - 22 Mar 2006 18:31 GMT
Assuming that you're looking for a public property of type string, you can
do something like this...

Dim info As PropertyInfo = Me.Button1.GetType().GetProperty("Text")
If (Not info Is Nothing) Then
 Dim obj As Object = info.GetValue(Me.Button1, Nothing)
 If (TypeOf obj Is String) Then
   MessageBox.Show(Convert.ToString(obj))
 End If
End If

Signature

Tim Wilson
.NET Compact Framework MVP

> Ok...I think I see where this is going, but I'm still having some issues.
>
[quoted text clipped - 26 lines]
> > >
> > > There's got to be a better way then this.  Is Reflection what I'm after?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.