Results 1 to 24 of 24

Thread: Compile Error?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Iowa
    Posts
    96

    Compile Error?

    For the life of me I cannot figure out this error...

    Else without If


    I'm writing a simple Tic Tac Toe Program where users take turn putting their x's or o's on the board...I'm still in the stage of deciding which letter gets placed. here is my code...I have the same thing for all Commands, except that they change a differant commands caption it corrosponds...command 1 is the upper left box 2 is upper middle and so on right to left all the way down...

    VB Code:
    1. Public Sub Form_Load()
    2. Dim whoturn As Boolean
    3. whoturn = False
    4. End Sub
    5.  
    6. Public Sub Command1_Click()
    7. If whoturn = False Then whoturn = True
    8. Else: whoturn = False
    9. If whoturn = True Then Command1.Caption = "X"
    10. Else
    11. Command1.Caption = "O"
    12. End Sub

    Again i get the above error, and Idk what i'm doing wrong...any suggestions??
    It is like wiping your ass with silk, I love it!

  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Else: and Else..... remove the semicolon

    and how are you comparing whoturn anyway? That variable is local to the forum load sub only, you can't compare it in another form if you use dim

  3. #3
    Lively Member
    Join Date
    May 2003
    Location
    Los Angeles
    Posts
    126
    The : indicates Go to a label . The proper way to write an if ... then...ElseIf... Else statement is ....

    If <Condition> = <evaluator> Then
    Do Something
    Else if <Condition> = <Evaluator> then
    Do something different
    Else
    Do this
    End If

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Iowa
    Posts
    96
    blah no tha'ts not the problem

    i have here is my code...

    and i get the same error

    VB Code:
    1. Public whoturn as boolean
    2.  
    3. Public Sub Form_Load()
    4. Dim whoturn As Boolean
    5. whoturn = False
    6. End Sub
    7.  
    8. Public Sub Command1_Click()
    9. If whoturn = False Then whoturn = True
    10. Else
    11. whoturn = False
    12. If whoturn = True Then Command1.Caption = "X"
    13. Else
    14. Command1.Caption = "O"
    15. End Sub
    It is like wiping your ass with silk, I love it!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Iowa
    Posts
    96
    IGBP so i need

    If Then
    Else If
    Else
    End If

    I need all of that?
    It is like wiping your ass with silk, I love it!

  6. #6
    Junior Member
    Join Date
    Nov 2003
    Posts
    17
    Use this

    VB Code:
    1. Public whoturn as boolean
    2.  
    3. Public Sub Form_Load()
    4. Dim whoturn As Boolean
    5. whoturn = False
    6. End Sub
    7.  
    8. Public Sub Command1_Click()
    9. If whoturn = False Then
    10. whoturn = True
    11. Else
    12. whoturn = False
    13. If whoturn = True Then
    14. Command1.Caption = "X"
    15. Else
    16. Command1.Caption = "O"
    17. End Sub

    You cannot have Else , if you are using the If statement in one line....

    Nandu

  7. #7
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by thurst0n
    IGBP so i need

    If Then
    Else If
    Else
    End If

    I need all of that?
    Yes you have to END the if statement.(or is that only .NET?)

    Also, you have a public variable and a local variable, both with the samename. That is a no no, use 1 or the other

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Iowa
    Posts
    96
    public local? what do you mean can u tell me what to do then explain why i have to do it...i'm really confused i've been working on this what YOU would considere simple thing for like 1.5 hours
    It is like wiping your ass with silk, I love it!

  9. #9
    Junior Member
    Join Date
    Nov 2003
    Posts
    17
    Look at my post above and let me know if that helps.........

    Nandu

  10. #10
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by thurst0n
    public local? what do you mean can u tell me what to do then explain why i have to do it...i'm really confused i've been working on this what YOU would considere simple thing for like 1.5 hours
    Buy a book, scope is a very easy concept

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Iowa
    Posts
    96
    get-Nope it doesnt work, it does the same thing That mine did (btw that's what i had originally i was playign with it and copied it wrong)
    It is like wiping your ass with silk, I love it!

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Iowa
    Posts
    96
    Ok, I've tried everything, it simply doesnt work i keep getting the same error Else witout If, Would someone please write the code (it's short) and post it and tell me why it happens
    It is like wiping your ass with silk, I love it!

  13. #13
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by thurst0n
    Ok, I've tried everything, it simply doesnt work i keep getting the same error Else witout If, Would someone please write the code (it's short) and post it and tell me why it happens
    Post the code you're using

    This works in .NET:
    VB Code:
    1. Public whoturn As Boolean
    2.  
    3.     Public Sub Form_Load()
    4.         whoturn = False
    5.     End Sub
    6.  
    7.     Public Sub Command1_Click()
    8.         If whoturn = False Then
    9.             whoturn = True
    10.         Else
    11.             whoturn = False
    12.         End If
    13.         If whoturn = True Then
    14.             Command1.Caption = "X"
    15.         Else
    16.             Command1.Caption = "O"
    17.         End If
    18.     End Sub

    I don't have VB 6 in front of me, but I doubt it's much different.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Iowa
    Posts
    96
    I did post the error...I post exactly what i had, and exactly what it said...
    It is like wiping your ass with silk, I love it!

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Iowa
    Posts
    96
    What you have works, but it's not toggling it, it doesnt switch to O's when i press a differant button...
    It is like wiping your ass with silk, I love it!

  16. #16
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by thurst0n
    What you have works, but it's not toggling it, it doesnt switch to O's when i press a differant button...
    Form.Update()

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Iowa
    Posts
    96
    What? Where?
    It is like wiping your ass with silk, I love it!

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Iowa
    Posts
    96
    = expected
    It is like wiping your ass with silk, I love it!

  19. #19
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Try this:

    VB Code:
    1. Public whoturn As Boolean
    2.  
    3. Option Explicit
    4.  
    5.     Public Sub Form_Load()
    6.         whoturn = False
    7.     End Sub
    8.    
    9.  
    10. Public Sub Command1_Click()
    11.    
    12.     whoturn = Not whoturn
    13.     Command1.Caption = IIf(whoturn, "X", "O")
    14.    
    15. End Sub
    Please rate my post.

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Iowa
    Posts
    96
    The option explicit fixes it, I can keep my other code..What does Option Explicit mean/do?
    It is like wiping your ass with silk, I love it!

  21. #21
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    "Option Explicit" means that all variables must be declared.

    If you type a variable name wrong it won't let the code run (without it the code would run, but might give the wrong results).

  22. #22

  23. #23
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    thurst0n

    Boy, that's the beginning of spagetti code if ever I saw any. Who's got the sauce!

    Try

    VB Code:
    1. whoturn = Not whoturn  ' Flip the switch - true to false / false to true
    2.  
    3. If whoturn then
    4.    Command1.Caption = "X"
    5. Else
    6.    Command1.Caption = "O"
    7. End if

  24. #24
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    if you didn't know

    if...then's can either be block or inline, as in

    if blah = 1 then msgbox "hello"

    or

    if blah = 1 then
    msgbox "hello"
    end if

    note that the first one does not require an end if, but it only works on one line (the things after 'then')
    the second one can be used on multiple lines until the end if.

    more stuff

    if blah = 1 then msgbox "hello" else msgbox "bye"

    and

    if blah = 1 then
    msgbox "hello"
    else
    msgbox "bye"
    end if

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