Search code examples
vb.netvisual-studiovariablesdynamic-variables

Parse Control name to Variable name


I have a var called as "Cheat_Enabled":

 Dim Cheat_Enabled As Boolean

And a Checkbox called as "CheckBox_Cheat" with the tag "Cheat"

Now I want to do a dynamic method to change the value of the var by spliting (or something) the name of the control.

For example, something like this (the code obviouslly don't work):

Private Sub CheckBoxes_CheckedChanged(sender As Object, e As EventArgs) Handles _
    CheckBox_Cheat.CheckedChanged

    Dim SenderVarEquivalent As Object = _
    Me.variables.Find(sender.Tag & "_Enabled")(0)

    If sender.Checked Then SenderVarEquivalent = True _
    Else SenderVarEquivalent = False

End Sub

    ' Info:
    ' Sender.Tag = "Cheat"
    ' Sender.Name = "CheckBox_Cheat"
    ' VariableName = "Cheat_Enabled"

I think this can be done with one CType or DirectCast or GetObject but I don't know exactly how to do it.

UPDATE:

This is a similar easy code but instead of converting ControlName to VariableName it is converting the control tag to the ControlName Object of the Checkbox:

Public Class Form1

    Dim Cheat_Enabled As Boolean = True

    Private Sub CheckBox_Cheat_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox_Cheat.CheckedChanged
        CType(Controls("CheckBox" & "_" & sender.tag), CheckBox).Checked = Cheat_Enabled
    End Sub

End Class

I hope if I can do the same to CType the control name to catch the variablename and use it, for example like this:

    Dim Cheat_Enabled As Boolean

Private Sub CheckBox_Cheat_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox_Cheat.CheckedChanged

    ' Sender.name is  : "CheckBox_Cheat"
    ' Sender.tag is   : "Cheat"
    ' Variable name is: "Cheat_Enabled"

    If sender.checked Then
         CType(Variable(sender.tag & "_Enabled"), Boolean) = True
    Else
         CType(Variable(sender.tag & "_Enabled"), Boolean) = False
    End If

End Sub

Solution

  • In MSDN you can find a VB.Net example how to enumerate all members of a given class:

    Imports System
    Imports System.Reflection
    Imports Microsoft.VisualBasic
    
    Class MyFindMembersClass
    
        Public Shared Sub Main()
            Dim objTest As New Object()
            Dim objType As Type = objTest.GetType()
            Dim arrayMemberInfo() As MemberInfo
            Try
                'Find all static or public methods in the Object 
                'class that match the specified name.
                arrayMemberInfo = objType.FindMembers(MemberTypes.Method, _
                                  BindingFlags.Public Or BindingFlags.Static _
                                  Or BindingFlags.Instance, _
                                  New MemberFilter(AddressOf DelegateToSearchCriteria), _
                                  "ReferenceEquals")
    
                Dim index As Integer
                For index = 0 To arrayMemberInfo.Length - 1
                    Console.WriteLine("Result of FindMembers -" + ControlChars.Tab + _
                                   arrayMemberInfo(index).ToString() + ControlChars.Cr)
                Next index
            Catch e As Exception
                Console.WriteLine("Exception : " + e.ToString())
            End Try
        End Sub 'Main
    
        Public Shared Function DelegateToSearchCriteria _
                                (ByVal objMemberInfo As MemberInfo, _
                                 ByVal objSearch As Object) As Boolean
            ' Compare the name of the member function with the filter criteria.
            If objMemberInfo.Name.ToString() = objSearch.ToString() Then
                Return True
            Else
                Return False
            End If
        End Function 'DelegateToSearchCriteria 
    End Class 'MyFindMembersClass
    

    Another alternative would be to put all your boolean variables into one Dictionary object. This would allow you to access the boolean values "by name" without using Reflection.

    Example for illustration:

    Imports System.Collections.Generic
    
    Module vbModule
    
    Class Example
    
        Private _dictionary
    
        Public Sub New()
            ' Allocate and populate the field Dictionary.
            Me._dictionary = New Dictionary(Of String, Boolean)
            Me._dictionary.Add("v1", False)
            Me._dictionary.Add("v2", True)
        End Sub
    
        Public Function GetValue(name as String) As Boolean
            ' Return value from private Dictionary.
            Return Me._dictionary.Item(name)
        End Function
    
        Public Sub SetValue(name as String, val as Boolean)
            Me._dictionary.Item(name) = val
        End Sub
    
    End Class
    
    Sub Main()
        ' Allocate an instance of the class.
        Dim example As New Example
    
        ' Write a value from the class.
        Console.WriteLine(example.GetValue("v1"))
        Console.WriteLine(example.GetValue("v2"))
        example.SetValue("v1", true)
        Console.WriteLine(example.GetValue("v1"))
    End Sub
    End Module