views:

455

answers:

3

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?

A: 

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
Joel Coehoorn
Unless you have OPTION BASE 1, your array will have 5 elements (one more than is required). Also, your for loop won't compile.
G Mastros
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.
Joel Coehoorn
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)
G Mastros
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.
Joel Coehoorn
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 elementsDim i As VariantFor Each i In L If "Foo" <> i Then ''// ... End IfNext i
MarkJ
This is why I always HATED vb6, but don't mind VB.Net so much ;)
Joel Coehoorn
What an absolutely awful statement, especially in the context of a VB6 question. I'm very disappointed.
G Mastros
+3  A: 

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
G Mastros
Awesome! I will try that and let you know if it worked. Thanks for such a quick response.
This worked perfect for me. Thanks!
A: 

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
onedaywhen
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 :)
MarkJ
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 ;-)
onedaywhen
Don't get me wrong, your answer is the coolest!
MarkJ