|
-
Sep 10th, 2007, 09:02 AM
#1
Thread Starter
Addicted Member
[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
Last edited by Fonty; Sep 10th, 2007 at 04:50 PM.
-
Sep 10th, 2007, 09:13 AM
#2
Re: Loops.
One thing that immediately jumps out is that you forgot an "AS"
Code:
Dim auxAs Object
'should be
Dim auxAs As Object
-
Sep 10th, 2007, 09:15 AM
#3
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
-
Sep 10th, 2007, 10:32 AM
#4
Thread Starter
Addicted Member
Re: Loops.
name property was just an example. In fact my interest is to extract the value of that string.
-
Sep 10th, 2007, 12:07 PM
#5
-
Sep 10th, 2007, 12:09 PM
#6
Thread Starter
Addicted Member
-
Sep 10th, 2007, 12:11 PM
#7
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
-
Sep 10th, 2007, 12:27 PM
#8
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
-
Sep 10th, 2007, 12:37 PM
#9
Re: Loops.
Well, you are right about that. I certainly did not get that out of what was originally asked.
-
Sep 10th, 2007, 04:04 PM
#10
Thread Starter
Addicted Member
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...
-
Sep 10th, 2007, 04:12 PM
#11
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
-
Sep 10th, 2007, 04:47 PM
#12
Thread Starter
Addicted Member
Re: Loops.
I solved it perfectly with an array. Thanks a lot.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|