Property list

  • Thread starter Thread starter Raymond Lewallen
  • Start date Start date
R

Raymond Lewallen

Want to know if/how to get a list of properties that are available in a
class and store the properties names in an arraylist.

TIA,

Raymond Lewallen
 
* "Raymond Lewallen said:
Want to know if/how to get a list of properties that are available in a
class and store the properties names in an arraylist.

\\\
Me.ListBox1.DisplayMember = "Name"
Me.ListBox1.DataSource = GetType(SystemColors).GetProperties()
///
 
Ok, what I thought I had figured out isn't working. Can anyone tell me why
my lenght for propertyinfo always comes up zero?

Public Class PdfSupport
Shared Function ReflectPropertyNamesToArrayList(ByVal type As Type, _
ByRef al As ArrayList) As ArrayList
Dim propertyInformation As PropertyInfo() = _
type.GetProperties(BindingFlags.Public)
Dim propertyCount As Int32
For propertyCount = 0 To propertyInformation.Length - 1
Dim currentProperty As PropertyInfo = _
CType(propertyInformation(propertyCount), PropertyInfo)
al.Add(currentProperty.Name.ToString)
Next propertyCount
Return al
End Function
End Class

Public Class Retrieve

Dim _al As New ArrayList

Public Sub New()
Load()
End Sub

Private Sub Load()

LoadArrayList()

End Sub

Private Sub LoadArrayList()
_al =
PdfSupport.ReflectPropertyNamesToArrayList(GetType(MyClass), _al)
End Sub

End Class

Friend Class MyClass

Public ReadOnly Property A() As String
Get
Return "Hello"
End Get
End Property

Public ReadOnly Property B() As String
Get
Return "World"
End Get
End Property

Public ReadOnly Property C() As String
Get
Return "How"
End Get
End Property

Public ReadOnly Property D() As String
Get
Return "Are"
End Get
End Property

Public ReadOnly Property E() As String
Get
Return "You"
End Get
End Property

End Class
 
I found that part in reflection, but I'm trying to get it to work by passing
the type into a function as a parameter, with no success. See my other post
in this topic.
 
Raymond Lewallen said:
I found that part in reflection, but I'm trying to get it to work
by passing the type into a function as a parameter, with no
success. See my other post in this topic.

\\\
ListAllTypes(Me.GetType(), Me.ListBox1)
..
..
..
Private Sub ListAllTypes(ByVal Type As Type, ByVal ListBox As ListBox)
ListBox.DisplayMember = "Name"
ListBox.DataSource = Type.GetProperties()
End Sub
///
 
Raymond,
Ok, what I thought I had figured out isn't working. Can anyone tell me why
my lenght for propertyinfo always comes up zero?
Because your GetProperties call is returning only members that have only the
Public attribute set. At the very least you also need to include
BindingFlags.Instance if you want instance properties & possible
BindingFlags.Static if you want Shared properties. Be certain to read the
Type.GetProperties(bindingFlags) help to find out how to call it. I would
simply use Type.GetProperties().


However I have a few questions comments about your code:
Shared Function ReflectPropertyNamesToArrayList(ByVal type As Type, _
ByRef al As ArrayList) As ArrayList

Why return the array via both the parameters & the return value?

I would change the function to:
Shared Function ReflectPropertyNamesToArrayList(ByVal type As Type _
) As ArrayList
Dim al As New ArrayList

Note ArrayList is a Reference type, passing it as ByRef is redundent. ByRef
allows ReflectPropertyNamesToArrayList to modify the _al variable itself,
the ArrayList object itself is automatically modifiable by virtue of being a
Reference type.


Why are you calling ToString on a String property?
al.Add(currentProperty.Name.ToString)

I would change it to (as Name is already a string).
al.Add(currentProperty.Name)


Why are you creating an instance of the ArrayList, just to ignore it when
you call ReflectPropertyNamesToArrayList?

I would not use the New keyword on the declaration of _al:
Public Class Retrieve

Dim _al As ArrayList
_al =
PdfSupport.ReflectPropertyNamesToArrayList(GetType(MyClass))

Hope this helps
Jay
 
Jay,

Thanks for all your input. In answer to all your questions and comments,
its simple. I'm a DBA, not a programmer. :) I am however trying my best to
learn all this .NET stuff.

Raymond Lewallen
 
Back
Top