Iterate through Variable names

bill mauri
bill mauri used Ask the Experts™
on
I want to iterate through variable names without using Arrays, Lists, or other collections if possible.

Dim My_1_Var as string, My_2_Var as string, My_3_Var as string, ... My_n_Var as string
Dim My_1_Value as string, My_2_Value as string, My_3_Value as string, ... My_n_Value as string

For i = 1 to 15
   if My_(i)_Var = "" then My_(i)_Value = "Test_" & i
next i
....
....
For i = 1 to 15
  debug.print ("My Variable " & i & " = " & My_(i)_Var & vbtab & " My Value " & i & " = " & My_(i)_Value )
next i
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Just use Reflection; e.g. -
Imports System.Reflection
Imports System.Runtime.CompilerServices

Module Module1
    Sub Main()
        Dim instance As New MyFieldsClass
        For Each field In instance.GetFields()
            Console.WriteLine(field)
        Next
        Console.ReadLine()
    End Sub
End Module

Class MyFieldsClass
    Dim My_1_Var As String, My_2_Var As String, My_3_Var As String
    Dim My_1_Value As String, My_2_Value As String, My_3_Value As String
End Class

Module Extensions
    <Extension()>
    Public Iterator Function GetFields(Of T)(source As T) As IEnumerable(Of String)
        For Each field As FieldInfo In GetType(T).GetFields(BindingFlags.IgnoreCase Or BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.Static)
            If field.Name.IndexOf("var", StringComparison.OrdinalIgnoreCase) <> -1 Then
                Dim valField = GetType(T).GetField(field.Name.ToLower().Replace("var", "value"), BindingFlags.IgnoreCase Or BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.Static)
                If valField IsNot Nothing Then
                    Dim value As String = If(field.GetValue(source) IsNot Nothing, field.GetValue(source).ToString(), String.Format("Test_{0}", field.Name.Split("_")(1)))
                    valField.SetValue(source, value)
                    Yield String.Format("{1} = {2}{0}{3} = {4}", vbTab, field.Name, If(field.GetValue(source) IsNot Nothing, field.GetValue(source), ""), valField.Name, valField.GetValue(source))
                End If
            End If
        Next
    End Function
End Module

Open in new window

Which produces the following output -Capture.PNG
-saige-
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Here's another example using Reflection showing how to get/set the values using their specific names:
Public Class Form1

    Private My_1_Var As String, My_2_Var As String, My_3_Var As String,
        My_4_Var As String, My_5_Var As String, My_6_Var As String,
        My_7_Var As String, My_8_Var As String, My_9_Var As String,
        My_10_Var As String, My_11_Var As String, My_12_Var As String,
        My_13_Var As String, My_14_Var As String, My_15_Var As String

    Private My_1_Value As String, My_2_Value As String, My_3_Value As String,
         My_4_Value As String, My_5_Value As String, My_6_Value As String,
         My_7_Value As String, My_8_Value As String, My_9_Value As String,
         My_10_Value As String, My_11_Value As String, My_12_Value As String,
         My_13_Value As String, My_14_Value As String, My_15_Value As String

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim value As String

        For i As Integer = 1 To 15
            value = GetField("My_" & i & "_Var")
            If value = "" Then
                SetField("My_" & i & "_Value", "Test_" & i)
            End If
        Next

        Dim varName, valName As String
        For i As Integer = 1 To 15
            varName = "My_" & i & "_Var"
            valName = "My_" & i & "_Value"
            Debug.Print(varName & " = " & Chr(34) & GetField(varName) & Chr(34) & " / " & valName & " = " & Chr(34) & GetField(valName) & Chr(34))
        Next
    End Sub

    Private Function GetField(ByVal FieldName As String) As Object
        Return Me.GetType.GetField(FieldName, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Public).GetValue(Me)
    End Function

    Private Sub SetField(ByVal FieldName As String, ByVal value As Object)
        Me.GetType.GetField(FieldName, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Public).SetValue(Me, value)
    End Sub

End Class

Open in new window


Example output:
My_1_Var = "" / My_1_Value = "Test_1"
My_2_Var = "" / My_2_Value = "Test_2"
My_3_Var = "" / My_3_Value = "Test_3"
My_4_Var = "" / My_4_Value = "Test_4"
My_5_Var = "" / My_5_Value = "Test_5"
My_6_Var = "" / My_6_Value = "Test_6"
My_7_Var = "" / My_7_Value = "Test_7"
My_8_Var = "" / My_8_Value = "Test_8"
My_9_Var = "" / My_9_Value = "Test_9"
My_10_Var = "" / My_10_Value = "Test_10"
My_11_Var = "" / My_11_Value = "Test_11"
My_12_Var = "" / My_12_Value = "Test_12"
My_13_Var = "" / My_13_Value = "Test_13"
My_14_Var = "" / My_14_Value = "Test_14"
My_15_Var = "" / My_15_Value = "Test_15"

Open in new window

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
No feedback from author.  Both solutions fit the bill; split evenly.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial