-
[RESOLVED] Loops.
I'm trying to implement a loop on a set of variables of the same type which are named strn, where n is an integer.
Why does this code doesn't work? How should I define the intermediate variable aux?
Code:
Dim auxAs Object
Dim str1 As String
Dim str2 As String
str1 = "str1"
str2 = "str2"
For i = 1 To 2
Set obj= "Str" & 1
MsgBox aux.Name
Next i
-
Re: Loops.
One thing that immediately jumps out is that you forgot an "AS"
Code:
Dim auxAs Object
'should be
Dim auxAs As Object
-
Re: Loops.
And, I don't see why you need it anyway as you are not using auxAs for anything.
This worked, although I'm not completely sure what you are trying to do
Code:
Private Sub Command1_Click()
Dim str1 As String
Dim str2 As String
Dim i As Long
str1 = "str1"
str2 = "str2"
For i = 1 To 2
str1 = "Str" & 1
MsgBox str1
Next i
End Sub
-
Re: Loops.
name property was just an example. In fact my interest is to extract the value of that string.
-
Re: Loops.
-
Re: Loops.
-
Re: Loops.
Try this
Code:
Private Sub Command1_Click()
Dim str1 As String
Dim intCounter As Integer
Dim i As Long
str1 = "str1"
For i = 1 To 2
intCounter = intCounter + 1
str1 = "Str" & intCounter
MsgBox str1
Next
End Sub
-
Re: Loops.
I see this a LOT in here... and everytime, people don't quite get what the poster is asking for.... mainly because it's not possible. You can't create a variable to hold a variable's name and expect to get the variable's value.
Going back to the OP's original code, I'll make a change, then state what I think the OP is looking for, and see if I've got it right.
Code:
Dim str1 As String
Dim str2 As String
str1 = "Hack"
str2 = "TechGnome"
For i = 1 To 2
Set obj= "Str" & 1
MsgBox obj
Next i
I'm guessing that in this case the OP would expect "Hack" in the first message box and "TechGnome" in the second box.
Yeah, it doesn't work that way. What you would get is "Str1" followed by "Str2"... not quite what you are looking for is it?
There are ways to accomplish what you are doing, but not in the simplistic manner that you want - hint: Arrays, collections.
-tg
-
Re: Loops.
Well, you are right about that. I certainly did not get that out of what was originally asked. :eek:
-
Re: Loops.
Techgnome is right on how the code is supposed to work. However I still cannot make it work.
You can't set a object that way...
-
Re: Loops.
Right.... it just doesn't work that way. IF you set something dimmed as object to the literal text string "Str1"... that's exactly what it will contain.... actually, I think you may even get an error, about TypeConversion.
But at any rate, in a simplistic manner, what you are looking to do cannot be done. You'll have to use something else. Hint: Array or Collection
-tg
-
Re: Loops.
I solved it perfectly with an array. Thanks a lot.