Display all members of class using a for loop vb.net code example

Example: Display all members of class using a for loop vb.net

Public Class [MyClass]
    Private a As String
    Private b As String

    Public Sub New(a As String, b As String)
        Me.a = a
        Me.b = b
    End Sub

    Public Overrides Function ToString() As String
        Dim sb = New StringBuilder()
        For Each item In [GetType]().GetFields(BindingFlags.NonPublic Or BindingFlags.Instance)
             sb.Append(item.Name) _
               .Append("=") _
               .Append(item.GetValue(Me)) _
               .Append(" ")
        Next
        Return sb.ToString()
    End Function

Tags:

Misc Example