0

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
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • Can you explain why you need to do this? As Alex pointed out, it's possible to do, but it's most likely unnecessary. It's quite likely that there is a much better way of doing this in your situation. Reflection should only be used when there is no better way available. For instance, anywhere you need to know whether or not it is currently enabled, why not just check the value of the check box instead of that variable? Or, if you need the variable, why not just set `Cheat_Enabled = CheckBox_Cheat.Checked` in the `CheckedChanged` event? – Steven Doggart Apr 12 '13 at 12:42
  • Because I haVe a lot of controls, with a lot of tags, and a lot of boolean vars, that's the reason why I was looking for a dynamic way to modify the vars in the checkedchanged event instead of doing it like what you've said. thanks for comment! – ElektroStudios Apr 12 '13 at 18:28

1 Answers1

2

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
Axel Kemper
  • 10,544
  • 2
  • 31
  • 54
  • Thankyou so much, i'm not an expert and I can't understand how to use the MSDN example for my purposses, can you give me an MSDN example or info about the other alternative (dictionary) ? maybe I'll have more luck with that way or if you can extend the reflection example please. – ElektroStudios Apr 12 '13 at 10:44
  • is better to use a dictionary than a hastable? or no matter? – ElektroStudios Apr 12 '13 at 18:56
  • The common aspects and differences are discussed here: http://stackoverflow.com/questions/301371/why-is-dictionary-preferred-over-hashtable – Axel Kemper Apr 12 '13 at 22:24