0

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?

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! I will try that and let you know if it worked. Thanks for such a quick response.
0

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

7 Comments

Unless you have OPTION BASE 1, your array will have 5 elements (one more than is required). Also, your for loop won't compile.
It's been a LONG while since I've used vb6, but I seem to recall that base 1 was the default? I'll look up the old for loop syntax and fix it.
It's been... uh... I was just messing with arrays in VB6. :) Base defaults to 0. Usually, it's best (when you don't know the bounds of the array) to do this: For i = LBound(L) To UBound(L)
Okay, this should at least be clearly correct. I think I prefer your code in this case, though, since it will allow assignment on the array nodes.
That's still not VB6, sorry. You can't do "For Each i As String in L" in VB6. Needs to be the following. Dim L(3) As String ''// index begins at 0, 4 total elements Dim i As Variant For Each i In L If "Foo" <> i Then ''// ... End If Next i
|
0

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

3 Comments

THANK YOU! This is truly what I was looking for. However, what the other guys suggested will work with less code for my current project, I still wanted to know how to do this for future reference.
CallByName can be useful, but I still think DrPut needs to learn about arrays. Someone else might have to look at his code one day you know :)
Yes, I too thought of an array. You could say mine is the more literal answer. Others have merely (rightly) pointed out that the question is wrong ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.