Results 1 to 4 of 4

Thread: Problem with random and array

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2006
    Posts
    2

    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:
    1. Dim rnumber As Integer
    2. Randomize
    3. rnumber = Int(Rnd * 5) + 1
    4.  
    5.  
    6. los = "names" & rnumber
    7.  
    8.  
    9. names1 = Array("Tom", "Arnold", "John")
    10. names2 = Array("Tom2", "Arnold2", "John2", "ddd")
    11. names3 = Array("Tom3", "Arnold3", "John3", "dfsdfsdfs")
    12. names4 = Array("Tom4", "Arnold4", "John4")
    13. names5 = Array("Tom5", "Arnold6", "John7")
    14.  
    15.   For i = 0 To UBound(los) 'in this moment I have error - Expected Array

    Best Regards

  2. #2
    Junior Member littlepd's Avatar
    Join Date
    Jun 2006
    Location
    Lewisville, TX
    Posts
    28

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2006
    Posts
    2

    Re: Problem with random and array

    Thank you very much. It's helps me.

    Best regards

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Problem with random and array

    You can try this as well:
    VB Code:
    1. Option Explicit
    2.  
    3. Dim arNames(1 To 5) As Variant
    4.  
    5. Private Sub Form_Load()
    6.     arNames(1) = Array("Tom", "Arnold", "John")
    7.     arNames(2) = Array("Tom2", "Arnold2", "John2", "ddd")
    8.     arNames(3) = Array("Tom3", "Arnold3", "John3", "dfsdfsdfs")
    9.     arNames(4) = Array("Tom4", "Arnold4", "John4")
    10.     arNames(5) = Array("Tom5", "Arnold6", "John7")
    11.    
    12.     Randomize
    13. End Sub
    14.  
    15. Private Sub Command1_Click()
    16. '==============================
    17. Dim iIndex As Integer
    18. Dim i As Integer
    19. Dim vNames As Variant
    20.  
    21.     iIndex = Int(Rnd * 5) + 1
    22.    
    23.     vNames = arNames(iIndex)
    24.     For i = 0 To UBound(vNames)
    25.         Debug.Print vNames(i)
    26.     Next i
    27.  
    28. 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
  •  



Click Here to Expand Forum to Full Width