Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleGet the values of fields (variables) declared in a form by their names in Visual Basic .NET
DescriptionThis example shows how to get the values of fields (variables) declared in a form by their names in Visual Basic .NET.
Keywordsreflection, FieldInfo, field, value, field value, FieldType, GetValue, VB.NET, Visual Basic .NET
CategoriesVB.NET, Software Engineering
 
When you select a field's name from the combo box, the program uses GetField to get information about the field. It then uses IsArray to see if the field is an array, and uses GetValue to get the field's value and treat it appropriately.
 
Private private_value1 As String = "This is private value 1"
Private private_value2 As String = "This is private value 2"
Public public_value1 As String = "This is public value 1"
Public public_value2 As String = "This is public value 2"
Public arr1() As String = {"A", "B", "C"}
Public arr2() As String = {"1", "2", "3"}

Private Sub cboFields_SelectedIndexChanged(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    cboFields.SelectedIndexChanged
    Dim field_info As FieldInfo = _
        Me.GetType().GetField(cboFields.Text, _
        BindingFlags.Instance Or BindingFlags.NonPublic Or _
            BindingFlags.Public)
    If field_info Is Nothing Then
        lblValue.Text = "<not found>"
    ElseIf field_info.FieldType.IsArray() Then
        ' Join the array values into a string.
        lblValue.Text = Join(field_info.GetValue(Me))
    Else
        ' Just convert it into a string.
        lblValue.Text = field_info.GetValue(Me).ToString()
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated