|
-
May 6th, 2006, 02:17 PM
#1
Thread Starter
Addicted Member
Cant use mid function on collection
VB Code:
Option Explicit
Dim Player As Boolean
Dim Space As Integer
Dim Win As Boolean
Dim Square(0 To 2) As String
Dim Wins As New Collection
Dim i As Integer
Dim j As Integer
Private Sub Form_Load()
For i = 0 To 8
B(i).Caption = ""
B(i).Value = vbChecked
Next i
With Wins
.Add "012"
.Add "345"
.Add "678"
.Add "036"
.Add "147"
.Add "258"
.Add "048"
.Add "246"
End With
End Sub
Private Sub B_Click(Index As Integer)
With B(Index)
.Value = vbChecked
If Win = False Then
If .Caption = "" Then
If Player = False Then
.Caption = "X"
Player = True
ElseIf Player = True Then
.Caption = "O"
Player = False
End If
End If
For j = 1 To 8
For i = 1 To 3
[B]Square(i - 1) = Mid(Wins(j), i, 1)[/B]
Next i
If B(Square(0)) = "X" And B(Square(1)) = "X" And B(Square(2)) = "X" Then
Win = True
MsgBox "Player X Won"
Exit For
ElseIf B(Square(0)) = "O" And B(Square(1)) = "O" And B(Square(2)) = "O" Then
Win = True
MsgBox "Player O Won"
Exit For
End If
Next j
For i = 0 To 8
If B(i).Caption = "" Then
Space = Space + 1
End If
Next i
If Space = 0 Then
Win = True
MsgBox "Tie"
End If
End If
End With
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?
-
May 6th, 2006, 02:29 PM
#2
Re: Cant use mid function on collection
Care to tell us what the error is, rather than have us guess at what it might be?
-
May 6th, 2006, 02:31 PM
#3
Addicted Member
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]
-
May 6th, 2006, 02:32 PM
#4
Thread Starter
Addicted Member
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?
-
May 6th, 2006, 02:34 PM
#5
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:
Private Sub Form_Load()
With Wins
.Add "012"
.Add "345"
.Add "678"
.Add "036"
.Add "147"
.Add "258"
.Add "048"
.Add "246"
End With
For i = 0 To 8
B(i).Caption = ""
B(i).Value = vbChecked
Next i
End Sub
Pradeep
-
May 6th, 2006, 02:34 PM
#6
Addicted Member
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]
-
May 6th, 2006, 02:38 PM
#7
Thread Starter
Addicted Member
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?
-
May 6th, 2006, 02:39 PM
#8
Re: Cant use mid function on collection
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|