Results 1 to 8 of 8

Thread: Cant use mid function on collection

  1. #1

    Thread Starter
    Addicted Member Piller's Avatar
    Join Date
    Oct 2004
    Location
    california
    Posts
    177

    Resolved Cant use mid function on collection

    VB Code:
    1. Option Explicit
    2. Dim Player As Boolean
    3. Dim Space As Integer
    4. Dim Win As Boolean
    5. Dim Square(0 To 2) As String
    6. Dim Wins As New Collection
    7. Dim i As Integer
    8. Dim j As Integer
    9.  
    10. Private Sub Form_Load()
    11. For i = 0 To 8
    12.     B(i).Caption = ""
    13.     B(i).Value = vbChecked
    14. Next i
    15. With Wins
    16.     .Add "012"
    17.     .Add "345"
    18.     .Add "678"
    19.     .Add "036"
    20.     .Add "147"
    21.     .Add "258"
    22.     .Add "048"
    23.     .Add "246"
    24. End With
    25. End Sub
    26.  
    27. Private Sub B_Click(Index As Integer)
    28. With B(Index)
    29. .Value = vbChecked
    30.     If Win = False Then
    31.         If .Caption = "" Then
    32.             If Player = False Then
    33.                 .Caption = "X"
    34.                 Player = True
    35.             ElseIf Player = True Then
    36.                 .Caption = "O"
    37.                 Player = False
    38.             End If
    39.         End If
    40.        
    41.         For j = 1 To 8
    42.             For i = 1 To 3
    43.                 [B]Square(i - 1) = Mid(Wins(j), i, 1)[/B]
    44.             Next i
    45.            
    46.             If B(Square(0)) = "X" And B(Square(1)) = "X" And B(Square(2)) = "X" Then
    47.                 Win = True
    48.                 MsgBox "Player X Won"
    49.                 Exit For
    50.             ElseIf B(Square(0)) = "O" And B(Square(1)) = "O" And B(Square(2)) = "O" Then
    51.                 Win = True
    52.                 MsgBox "Player O Won"
    53.                 Exit For
    54.             End If
    55.         Next j
    56.        
    57.         For i = 0 To 8
    58.             If B(i).Caption = "" Then
    59.                 Space = Space + 1
    60.             End If
    61.         Next i
    62.        
    63.         If Space = 0 Then
    64.             Win = True
    65.             MsgBox "Tie"
    66.         End If
    67.     End If
    68. End With
    69. End Sub
    (B is a checkbox that im keeping checked)
    This is a tic-tac-toe game, and instead of checked all the checkboxs to see if someone won, I decided to use a collection and parse out each integer and check that way. I keep getting a error on the bolded line though and im not sure why. The line looks fine for me and should produce the results Square(0) = 0, Square(1) = 1, and Square(2) = 2. Could anybody help me with this?
    Last edited by Piller; May 6th, 2006 at 02:38 PM.
    Are we alive or just breathing?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Cant use mid function on collection

    I keep getting a error
    Care to tell us what the error is, rather than have us guess at what it might be?

  3. #3
    Addicted Member
    Join Date
    Apr 2006
    Location
    USA
    Posts
    207

    Re: Cant use mid function on collection

    Hey Piller,

    Wins is a collection, your code is reading it as if it was an array.

    instead of:
    Square(i - 1) = Mid(Wins(j), i, 1)

    This should work:
    Square(i - 1) = Mid(Wins.Item(j), i, 1)
    Keith_VB6

    If you have any further questions, just ask.
    If this solves things, then please mark the thread resolved.
    [Thread Tools] --> [Mark Thread Resolved]

  4. #4

    Thread Starter
    Addicted Member Piller's Avatar
    Join Date
    Oct 2004
    Location
    california
    Posts
    177

    Re: Cant use mid function on collection

    It says

    Run-time Error '5':
    Invalid procedure call or argument.

    Sorry I forgot to mention in post.
    Are we alive or just breathing?

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Cant use mid function on collection

    This is because you are setting the Checked property of checkbox before initializing the Collection. Modify your Form_Load like this:
    VB Code:
    1. Private Sub Form_Load()
    2.     With Wins
    3.         .Add "012"
    4.         .Add "345"
    5.         .Add "678"
    6.         .Add "036"
    7.         .Add "147"
    8.         .Add "258"
    9.         .Add "048"
    10.         .Add "246"
    11.     End With
    12.     For i = 0 To 8
    13.         B(i).Caption = ""
    14.         B(i).Value = vbChecked
    15.     Next i
    16. End Sub

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6
    Addicted Member
    Join Date
    Apr 2006
    Location
    USA
    Posts
    207

    Re: Cant use mid function on collection

    Sorry about that, it can be read in either form. my bad.

    As si_the_geek said, what's the error message.
    Keith_VB6

    If you have any further questions, just ask.
    If this solves things, then please mark the thread resolved.
    [Thread Tools] --> [Mark Thread Resolved]

  7. #7

    Thread Starter
    Addicted Member Piller's Avatar
    Join Date
    Oct 2004
    Location
    california
    Posts
    177

    Re: Cant use mid function on collection

    Thanks Pradeep, I didnt know that it mattered what order the code was in.
    Are we alive or just breathing?

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Cant use mid function on collection

    Quote Originally Posted by Pradeep1210
    This is because you are setting the Checked property of checkbox before initializing the Collection.
    Pradeep
    My first thought was " ", but I can see that you are right - setting the Value property of the checkbox calls the _Click event, and as such runs that code before you have set up the collection.

    If you dont want the event to run until the form is loaded, have a boolean variable which you only set to True at the end of form_load. At the start of the event, exit the sub if the variable is False.

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