Reputation:
I am looping through some code using a For loop. The iterative variable is "i". I have dimensioned the following variables prior to the For Loop. L1, L2, L3, L4 as strings. I want to reference these strings inside the For loop by somehow referring to "L" & char(i). So like a comparison of a value "Foo" <> "L" & Char(i), should result is testing "Foo" against the string stored in variable L1, when i=1. Or against L2 when i=2 and so on.
My previous programming experience is Visual FoxPro and all I had to do was prefix an & on the front of the string and it then referenced the variable whose name is stored in the string.
So if L1 stored "Bar", and I wanted to compare I could write &L1 == "Bar". I need to be able to do this with VB6. Can anyone help?
Upvotes: 0
Views: 1308
Reputation: 57093
This works in a class (e.g. a VB Form):
Option Explicit
Public L1 As String
Public L2 As String
Public L3 As String
Public L4 As String
Sub Main()
L1 = "Foo"
L2 = "Bar"
L3 = "Go"
L4 = "Figure"
Dim i As Long
For i = 1 To 4
Debug.Print CallByName(Me, "L" & CStr(i), VbGet)
Next
End Sub
Upvotes: 0
Reputation: 416149
What you really want is an array, like this:
Dim L(3) As String ''// index begins at 0, 4 total elements
For Each i As String In L
If "Foo" <> i Then
''// ...
End If
Next i
Upvotes: 0
Reputation: 24506
Instead of creating 4 variables, I would suggest that you create an array. Ex:
Dim L(1 To 4) As String
For i = 1 to 4
L(i) = "Whatever"
Next
Upvotes: 3