Looping through the variables (properties?) of a class

Posted on 2005-03-09
Medium Priority
Last Modified: 2013-11-23
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
Question by:rbertke
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 13503353
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
            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"
                    p = p & "/Write"
                End If
            End If
            p = p & " Property " & pi.Name & "() of Type " & pi.PropertyType.ToString

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

End Class


Author Comment

ID: 13505519
yep, that works like a charm.  Thanks a ton!!!  Rob

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn different types of Android Layout and some basics of an Android App.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Six Sigma Control Plans

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question