title: Set variable with a loop using the variables name as a string
body: <p>My application requires variables to be set and retrieved multiple times in different forms and subs. Instead of writing the code multiple times i was hoping to use a loop to loop through the variables and set them accordingly. The code below shows my settings menu and how it is built. I'm stuck with setting the variables based on a name that is stored as a string (See last code snippet below).</p>
  
  <p>The section imedialty below are the menu classes. This bit works as expected</p>
  
  <pre><code>Public Class MenuItem
  
      Public Name As String
      Public Type As TypeEnum
  
      Enum TypeEnum
          CheckBox
          ComboBox
          TextBox
      End Enum     
  
      Public Sub New()
      End Sub
  
  End Class
  
  Public Class Menu
  
      Public MenuItems() As MenuItem
      Public Name As String
      Public LinkedVariable as Object
  
      Public Sub New()
      End Sub
  
      Public Sub AddMenuItem(ByVal ItemName As String, _
                             ByVal ItemType As MenuItem.TypeEnum, _
                             ByVal ItemLinkedVariable as Object)   
  
          If MenuItems Is Nothing Then
              ReDim MenuItems(0)
          Else
              ReDim Preserve MenuItems(MenuItems.Length)
          End If
          MenuItems(MenuItems.Length - 1) = New MenuItem
          MenuItems(MenuItems.Length - 1).Name = ItemName
          MenuItems(MenuItems.Length - 1).Type = ItemType
          MenuItems(MenuItems.Length - 1).LinkedVariable = ItemLinkedVariable 
      End Sub
  
  End Class
  </code></pre>
  
  <p>Build the menus in the main form class. This bit works as expected</p>
  
  <pre><code>Public Class Form1
      Public Settings As New SettingsForm
      Public Sub CreateMenu()
  
          Settings.AddMenu("Menu1")
  
          Settings.Menus(0).AddMenuItem("CheckBoxA", MenuItem.TypeEnum.CheckBox, VariableA)
          Settings.Menus(0).AddMenuItem("TextBoxB", MenuItem.TypeEnum.TextBox, VariableB)
          Settings.Menus(0).AddMenuItem("TextBoxC", MenuItem.TypeEnum.TextBox, VariableC) 
  
          Settings.AddMenu("Menu2")
  
          Settings.Menus(1).AddMenuItem("CheckBoxD", MenuItem.TypeEnum.CheckBox, VariableD)
          Settings.Menus(1).AddMenuItem("TextBoxE", MenuItem.TypeEnum.TextBox, VariableE)
  
          'etc......      
  
          Settings.Build()
  
          Settings.Show()
  
      End Sub
  End Class
  
  
  Public Class SettingsForm
      Public Menus() As Menu
  
      Public VariableA As Boolean
      Public VariableB As String
      Public VariableC As String
      Public VariableD As Boolean
      Public VariableE As String
  
      Public Sub New()
  
          InitializeComponent()
  
      End Sub
  
      Public Sub Build()
  
          SettingsTabControl.TabPages.Clear()
  
          If Menus IsNot Nothing Then
              For i As Integer = 0 To Menus.Length - 1
  
                  SettingsTabControl.TabPages.Add(Menus(i).Name)
                  SettingsTabControl.TabPages(i).Name = Menus(i).Name &amp; "TabPage"
                  SettingsTabControl.TabPages(i).Text = Menus(i).Name
  
                  AddSettingsItems(Menus(i), i)
  
              Next
          End If
  
      End Sub
  
      Public Sub AddSettingsItems(ByVal SelectedMenu As Menu, ByVal TabPageIndex As Integer)
          Dim y As Integer = 50
          Dim yinc As Integer = 25
  
          If SelectedMenu IsNot Nothing Then
              If SelectedMenu.MenuItems IsNot Nothing Then
  
                  For i As Integer = 0 To SelectedMenu.MenuItems.Length - 1
  
                      Dim formObj As Object
  
                      'Specific Settings
                      Select Case SelectedMenu.MenuItems(i).Type
                          Case MenuItem.TypeEnum.CheckBox
                              formObj = New CheckBox
                          Case MenuItem.TypeEnum.ComboBox
                              formObj = New ComboBox                            
                          Case MenuItem.TypeEnum.TextBox
                              formObj = New TextBox
                              formObj.TextAlign = HorizontalAlignment.Right
                              formObj.BorderStyle = BorderStyle.FixedSingle
                      End Select
  
                      formObj.Name = SelectedMenu.MenuItems(i).Name
                      formObj.Location = New Point(25, y)
                      SettingsTabControl.TabPages(TabPageIndex).Controls.Add(formObj)
  
                      y = y + yinc
                  Next
              End If
          End If
      End Sub
  End Class
  </code></pre>
  
  <p>This is the bit i am stuck on i want to be able to set the Variables A,B,C,D,E from the controls that were added to the SettingsTabControl. However i can't work out how to link the Variables to the LinkedVariable objects in each MenuItem. This is what i have but it doesn't work</p>
  
  <pre><code>Private Sub SettingsFromOkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click
  
      For m As Integer = 0 To Menus.Length - 1
          For i As Integer = 0 To Menus(m).MenuItems.Length
  
              Dim currentcontrol As Object = SettingsTabControl.TabPages(m).Controls(Menus(m).MenuItems(i).Name)
  
              Select Case Menus(m).MenuItems(i).Type
                  Case MenuItem.TypeEnum.CheckBox
                      If currentcontrol.CheckedState = True Then
                          Menus(m).MenuItems(i).LinkedVariable = True
                      Else
                          Menus(m).MenuItems(i).LinkedVariable = False
                      End If
                  Case MenuItem.TypeEnum.ComboBox
  
  
                  Case MenuItem.TypeEnum.TextBox
                      Menus(m).MenuItems(i).LinkedVariable = currentcontrol.Text
              End Select
  
          Next
      Next
  
      MsgBox(VariableA &amp; vbCrLf _
             VariableB &amp; vbCrLf _
             VariableC &amp; vbCrLf _
             VariableD &amp; vbCrLf _
             VariableE &amp; vbCrLf)
  
  End Sub
  </code></pre>