Looping through the variables (properties?) of a class

How can I loop through the variables inside a class?

I have a class such as:

Public Class MyTestClass
  Public DataField1 as string
  Public DataField2 as integer
End Class

I want a function that allows me to loop through the class and get a list with DataField1 and Datafield2 (and the data type if possible).  Anyone tell me how to do this?

Thanks!! Rob
rbertkeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
You can use reflection to get fields, properties, methods, events, etc...

Here is an example:

Public Class MyTestClass
    Private _DataField1 As String
    Public Property DataField1() As String
        Get
            Return _DataField1
        End Get
        Set(ByVal Value As String)
            _DataField1 = Value
        End Set
    End Property

    Public DataField2 As Integer
    Private PrivateDataField3 As Integer
End Class

Imports System.Reflection
Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListProperties(New MyTestClass)
    End Sub

    Private Sub ListProperties(ByVal o As Object)
        Dim p As String
        Dim pi As PropertyInfo
        Dim pis() As PropertyInfo = o.GetType.GetProperties
        For Each pi In pis
            If pi.CanRead Then
                p = "Read"
            End If
            If pi.CanWrite Then
                If p = "" Then
                    p = "Write"
                Else
                    p = p & "/Write"
                End If
            End If
            p = p & " Property " & pi.Name & "() of Type " & pi.PropertyType.ToString
            Debug.WriteLine(p)
        Next

        Dim fi As FieldInfo
        Dim fields() As FieldInfo = o.GetType.GetFields()
        For Each fi In fields
            Debug.WriteLine("Field: " & fi.Name)
        Next
    End Sub

End Class

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rbertkeAuthor Commented:
yep, that works like a charm.  Thanks a ton!!!  Rob
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.