Results 1 to 5 of 5

Thread: Sorting using the Mod function and control arrays

  1. #1

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692

    Sorting using the Mod function and control arrays

    I recently came across this bit of code inside a book I read:

    Sub Shuffle_Cards()
    Dim CardUsed(52) As Integer
    Dim J As Integer
    Call N_Integers(52, CardUsed())
    For J = 1 to 52
    Select Case (CardUsed(J) - 1) Mod 13 + 1
    Case 1
    CardName(J) = "A"
    CardValue(J) = 1
    Case 2
    CardName(J) = "2"
    CardValue(J) = 2
    Case 3
    CardName(J) = "3"
    CardValue(J) = 3
    Case 4
    CardName(J) = "4"
    CardValue(J) = 4
    Case 5
    CardName(J) = "5"
    CardValue(J) = 5
    Case 6
    CardName(J) = "6"
    CardValue(J) = 6
    Case 7
    CardName(J) = "7"
    CardValue(J) = 7
    Case 8
    CardName(J) = "8"
    CardValue(J) = 8
    Case 9
    CardName(J) = "9"
    CardValue(J) = 9
    Case 10
    CardName(J) = "10"
    CardValue(J) = 10
    Case 11
    CardName(J) = "J"
    CardValue(J) = 10
    Case 12
    CardName(J) = "Q"
    CardValue(J) = 10
    Case 13
    CardName(J) = "K"
    CardValue(J) = 10
    End Select
    CardSuit(J) = Int((CardUsed(J) - 1) / 13)
    Next J
    CurrentCard = 1
    End Sub

    I want to use a similar sorting algorithm with a control array. I put in this code:

    Sub Shuffle_Cards()
    Dim CardUsed(15) As Integer
    Dim CardsLeft As Integer
    Dim J As Integer
    Call N_Integers(15, CardUsed())
    For J = 1 To 15
    Select Case (CardUsed(J) - 1) Mod 15 + 1
    Case 1 To 5
    picNumber(Index).Picture = LoadPicture(App.Path + "\images\plus250.jpg")
    Case 6 To 10
    picNumber(Index).Picture = LoadPicture(App.Path + "\images\plus500.jpg")
    Case 11 To 15
    picNumber(Index).Picture = LoadPicture(App.Path + "\images\plus750.jpg")
    End Select
    CardsLeft(J) = Int((CardUsed(J) - 1) / 15)
    Next J

    Whoops, there's a problem: VB says the Index variable is undefined. How could I use this for a control array? I'd simply undo the control array, except I'd have about 50 picture boxes to write code for.

  2. #2
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Using Option Base 1 at the start of your Form/Module should clear it up. This is because VB defines Array(52) as Array(0 to 51) unless you tell it to use 1 as the base, therefore making it 1 to 52.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  3. #3

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    That didn't help. Index is still an undeclared variable.

  4. #4
    Zaei
    Guest
    Replace "Index" with "J".

    Z.

  5. #5
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Ah, hehe, missed that
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

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