PC Review


Reply
Thread Tools Rate Thread

Call a property using a string

 
 
Chad Miller
Guest
Posts: n/a
 
      5th Apr 2004
I have a class with 50 properties sp01 .. sp50 is there a way to call a
property using a string?
ex. o.sp & "01"

Chad


 
Reply With Quote
 
 
 
 
Armin Zingler
Guest
Posts: n/a
 
      5th Apr 2004
"Chad Miller" <(E-Mail Removed)> schrieb
> I have a class with 50 properties sp01 .. sp50 is there a way to call
> a property using a string?
> ex. o.sp & "01"


You can, but be careful, the compiler won't complain if a name is missing:
see the CallByName statment and the topic "Reflection":
http://msdn.microsoft.com/library/en...natruntime.asp

especially sub topic "Dynamically Loading and Using Types"


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

 
Reply With Quote
 
Chad Miller
Guest
Posts: n/a
 
      5th Apr 2004
Thanks, that worked great! It also taught me allot about the reflection
class.

"Charlie Williams" <(E-Mail Removed)> wrote in message
news:E4FE25E2-371A-4194-B40B-(E-Mail Removed)...
> You can accoplish this easily using reflection.
>
> Imports System.Reflection
>
> Public Function GetPropertyValueByName(ByVal num As String) As String
> Dim t As Type = Me.GetType()
> Dim pi As PropertyInfo = t.GetProperty("sp" & num)
> Return CType(pi.GetValue(Me, Nothing), String)
> End Function
>
> Charlie
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get Property value of class instance by passing string property name mrussell@bluebayinvest.com Microsoft Excel Programming 2 19th Oct 2007 05:47 PM
Call subroutine or set property on another form =?Utf-8?B?c3dhcw==?= Microsoft Access Form Coding 3 10th Sep 2007 12:26 PM
Setting property value to string or accessing property methods Brian Mitchell Microsoft VB .NET 2 26th May 2005 01:27 PM
Accessing a property using a string containing the property name? Don Microsoft VB .NET 4 8th Oct 2004 05:38 PM
Cannot create an object of type 'System.String[]' from its string representation 'String[] Array' for the 'Options' property. Hessam Microsoft C# .NET 0 8th Aug 2003 09:45 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:07 PM.