Reflection (I think)

G

Guest

I have a class that has 150 properties and I would like to, in additon to
calling the properties like myclass.property1, I would also like to be able
to call the property using an index string. For example;

Public Class myClass

public property prop1()

end property

end class

Public property getProperty(prop1index)
'What code can I use here to call property "Prop1"
end property

end class
Dennis in Houston
 
K

Ken Tucker [MVP]

Hi,

Test class



Public Class MyTestClass

Dim mstrA As String

Dim mstrB As String

Dim mintC As Integer

Public Property A() As String

Get

Return mstrA

End Get

Set(ByVal Value As String)

mstrA = Value

End Set

End Property

Public Property B() As String

Get

Return mstrB

End Get

Set(ByVal Value As String)

mstrB = Value

End Set

End Property

Public Property C() As Integer

Get

Return mintC

End Get

Set(ByVal Value As Integer)

mintC = Value

End Set

End Property

End Class



To get values



Dim c As New MyTestClass

c.A = "a"

c.B = "B"

c.C = 100

Dim t As Type = c.GetType

For Each pi As Reflection.PropertyInfo In t.GetProperties

Debug.WriteLine(String.Format("{0} {1} {2}", pi.Name, _

pi.PropertyType.FullName, pi.GetValue(c, Nothing)))

Next



Ken

----------------------

I have a class that has 150 properties and I would like to, in additon to
calling the properties like myclass.property1, I would also like to be able
to call the property using an index string. For example;

Public Class myClass

public property prop1()

end property

end class

Public property getProperty(prop1index)
'What code can I use here to call property "Prop1"
end property

end class
Dennis in Houston
 
O

One Handed Man \( OHM - Terry Burns \)

Good clean Answer Ken !

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
G

Guest

You might be interested in what I finally ended up with where:

'TagEdit is my class object name
' Artist is a property that returns a data structure of type TagEdit.TextData

Dim f as TagEdit = New TagEdit()
dim t as TagEdit.TxtData

Dim mymethod As MethodInfo =
GetType(TagEdit).GetProperty_("Artist").GetGetMethod

t = CType(mymethod.Invoke(f, Nothing), TagEdit.TxtData)

'It works! Now all I've got to do is figure out how to do this with
properties that have overloads with different input parameters..use Binding
flags I think!
 
J

Jonathan Allen

If you have 150 properties, you're probably going to have more in the
future. I suggest you ditch them all and try something different. Perhaps a
key/value pattern.

AddProperty(key as string, value as string)
GetProperty(key as string)
GetProperty(index as integer)
RemoveProperty(key as string)
 
G

Guest

Interesting suggestion but I'm not sure how to implement this. I assume this
code goes in the class but not sure how set/get each property in my
application that uses the class with this technique.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top