|
-
Dec 17th, 2006, 04:49 PM
#1
Thread Starter
New Member
Problem with random and array
Hi,
It's my first post on this board. OK It's a simple example of my problem. First App is randomizing number between 1..5. There are 5 arrays with diffrent data. And when I want to Print data from random array, I have error - Expected array
VB Code:
Dim rnumber As Integer
Randomize
rnumber = Int(Rnd * 5) + 1
los = "names" & rnumber
names1 = Array("Tom", "Arnold", "John")
names2 = Array("Tom2", "Arnold2", "John2", "ddd")
names3 = Array("Tom3", "Arnold3", "John3", "dfsdfsdfs")
names4 = Array("Tom4", "Arnold4", "John4")
names5 = Array("Tom5", "Arnold6", "John7")
For i = 0 To UBound(los) 'in this moment I have error - Expected Array
Best Regards
-
Dec 17th, 2006, 05:03 PM
#2
Junior Member
Re: Problem with random and array
You're asking for the number of the last entry in the array "los" (UBound(LOS)), but from what you have here, you have not declared "los" as an array. It's just a character string the contains the name of one of you five name arrays. Try this instead:
Code:
Select Case rnumber
Case 1: 'Use array "names1"
Case 2: 'Use array "names2"
Case 3: 'Use array "names3"
Case 4: 'Use array "names4"
Case 5: 'Use array "names5"
End Select
Welcome!
I used to have a handle on life, but it broke.
-
Dec 17th, 2006, 05:11 PM
#3
Thread Starter
New Member
Re: Problem with random and array
Thank you very much. It's helps me.
Best regards
-
Dec 17th, 2006, 05:11 PM
#4
Re: Problem with random and array
You can try this as well:
VB Code:
Option Explicit
Dim arNames(1 To 5) As Variant
Private Sub Form_Load()
arNames(1) = Array("Tom", "Arnold", "John")
arNames(2) = Array("Tom2", "Arnold2", "John2", "ddd")
arNames(3) = Array("Tom3", "Arnold3", "John3", "dfsdfsdfs")
arNames(4) = Array("Tom4", "Arnold4", "John4")
arNames(5) = Array("Tom5", "Arnold6", "John7")
Randomize
End Sub
Private Sub Command1_Click()
'==============================
Dim iIndex As Integer
Dim i As Integer
Dim vNames As Variant
iIndex = Int(Rnd * 5) + 1
vNames = arNames(iIndex)
For i = 0 To UBound(vNames)
Debug.Print vNames(i)
Next i
End Sub
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
|