Iterate through Variable names

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
bill mauriAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
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-
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonMiddle School Assistant TeacherCommented:
No feedback from author.  Both solutions fit the bill; split evenly.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.