467,169 Members | 961 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,169 developers. It's quick & easy.

Property list

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
Nov 20 '05 #1
  • viewed: 1471
Share:
7 Replies
Nevermind found the PropertyInfo type in reflection. Overlooked it before.

"Raymond Lewallen" <Ra******************@nospam.faa.gov> wrote in message
news:eA**************@tk2msftngp13.phx.gbl...
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

Nov 20 '05 #2
* "Raymond Lewallen" <Ra******************@nospam.faa.gov> scripsit:
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()
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
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
"Raymond Lewallen" <Ra******************@nospam.faa.gov> wrote in message
news:eA**************@tk2msftngp13.phx.gbl...
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

Nov 20 '05 #4
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.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2k************@uni-berlin.de...
* "Raymond Lewallen" <Ra******************@nospam.faa.gov> scripsit:
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()
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #5
"Raymond Lewallen" <Ra******************@nospam.faa.gov> schrieb:
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
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #6
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

"Raymond Lewallen" <Ra******************@nospam.faa.gov> wrote in message
news:er*************@tk2msftngp13.phx.gbl... 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
"Raymond Lewallen" <Ra******************@nospam.faa.gov> wrote in message
news:eA**************@tk2msftngp13.phx.gbl...
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


Nov 20 '05 #7
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

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

"Raymond Lewallen" <Ra******************@nospam.faa.gov> wrote in message
news:er*************@tk2msftngp13.phx.gbl...
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
"Raymond Lewallen" <Ra******************@nospam.faa.gov> wrote in message news:eA**************@tk2msftngp13.phx.gbl...
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



Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Vipin Kedia | last post: by
6 posts views Thread by Altman | last post: by
1 post views Thread by shapper | last post: by
1 post views Thread by shapper | last post: by
3 posts views Thread by shapper | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.