Results 1 to 15 of 15

Thread: [2005] if...else statements. please help.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    65

    [2005] if...else statements. please help.

    hi, i am having some problem with if...else statements. i have various if else statements which are controlld by 1 button.

    how can i make it, so that if 1 of the if...else statements is true, then it doesnt proceed onto the next if..else statement?

    the if else statements are as follows.

    Code:
            If (TextBox1.Text = "4" Or TextBox1.Text = "5" Or TextBox1.Text = "6" Or TextBox1.Text = "7" Or TextBox1.Text = "8" Or TextBox1.Text = "9" Or TextBox1.Text = "10") Xor _
                    (TextBox2.Text = "4" Or TextBox2.Text = "5" Or TextBox2.Text = "6" Or TextBox2.Text = "7" Or TextBox2.Text = "8" Or TextBox2.Text = "9" Or TextBox2.Text = "10") Xor _
                    (TextBox3.Text = "4" Or TextBox3.Text = "5" Or TextBox3.Text = "6" Or TextBox3.Text = "7" Or TextBox3.Text = "8" Or TextBox3.Text = "9" Or TextBox3.Text = "10") Xor _
                    (TextBox4.Text = "4" Or TextBox4.Text = "5" Or TextBox4.Text = "6" Or TextBox4.Text = "7" Or TextBox4.Text = "8" Or TextBox4.Text = "9" Or TextBox4.Text = "10") Xor _
                    (TextBox5.Text = "4" Or TextBox5.Text = "5" Or TextBox5.Text = "6" Or TextBox5.Text = "7" Or TextBox5.Text = "8" Or TextBox5.Text = "9" Or TextBox5.Text = "10") Then
                MsgBox("highcardplay")
            End If
    
    
    
            If (TextBox1.Text = "4" Or TextBox1.Text = "5" Or TextBox1.Text = "6" Or TextBox1.Text = "7" Or TextBox1.Text = "8" Or TextBox1.Text = "9" Or TextBox1.Text = "10") Xor _
            (TextBox2.Text = "4" Or TextBox2.Text = "5" Or TextBox2.Text = "6" Or TextBox2.Text = "7" Or TextBox2.Text = "8" Or TextBox2.Text = "9" Or TextBox2.Text = "10") Xor _
            (TextBox3.Text = "4" Or TextBox3.Text = "5" Or TextBox3.Text = "6" Or TextBox3.Text = "7" Or TextBox3.Text = "8" Or TextBox3.Text = "9" Or TextBox3.Text = "10") Xor _
            (TextBox9.Text = "4" Or TextBox9.Text = "5" Or TextBox9.Text = "6" Or TextBox9.Text = "7" Or TextBox9.Text = "8" Or TextBox9.Text = "9" Or TextBox9.Text = "10") Xor _
            (TextBox8.Text = "4" Or TextBox8.Text = "5" Or TextBox8.Text = "6" Or TextBox8.Text = "7" Or TextBox8.Text = "8" Or TextBox8.Text = "9" Or TextBox8.Text = "10") Then
                MsgBox("highcardcomp")
            End If
    
    
    
            If (TextBox1.Text = "1" Or TextBox1.Text = "2" Or TextBox1.Text = "3") Xor _
            (TextBox2.Text = "1" Or TextBox2.Text = "2" Or TextBox2.Text = "3") Xor _
            (TextBox3.Text = "1" Or TextBox3.Text = "2" Or TextBox3.Text = "3") Xor _
            (TextBox4.Text = "1" Or TextBox4.Text = "2" Or TextBox4.Text = "3") Xor _
            (TextBox5.Text = "1" Or TextBox5.Text = "2" Or TextBox5.Text = "3") Then
                MsgBox("flushplay")
            End If
    
    
    
            If (TextBox1.Text = "1" Or TextBox1.Text = "2" Or TextBox1.Text = "3") Xor _
            (TextBox2.Text = "1" Or TextBox2.Text = "2" Or TextBox2.Text = "3") Xor _
            (TextBox3.Text = "1" Or TextBox3.Text = "2" Or TextBox3.Text = "3") Xor _
            (TextBox9.Text = "1" Or TextBox9.Text = "2" Or TextBox9.Text = "3") Xor _
            (TextBox8.Text = "1" Or TextBox8.Text = "2" Or TextBox8.Text = "3") Then
                MsgBox("flushcomp")
            End If

    thanks

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [2005] if...else statements. please help.

    Maybe use an Exit Sub?

    Without knowing what you are trying to accomplish, its a little hard to make informed suggestions.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] if...else statements. please help.

    Exit Sub might well work, or the simpler Return. However, another alternative would be to add a single boolean variable. Set the variable to True inside any of the If statements, and check it in all the others. Thus, if the first If is True, the boolean gets set to True. The next If statement checks the boolean, and if the boolean is true, it fails that If statement.

    By the way, all those conditions look mutually exclusive, so shouldn't the first one be an If, and all the others be ElseIf statements? In the code you posted above, that would have the same result, but your subject line has me confused, because you mention ElseIf, yet you don't show one.

    One last note, you might want to use OrElse rather than Or, though I doubt it would make much difference in this case.
    My usual boring signature: Nothing

  4. #4
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [2005] if...else statements. please help.

    kapz20, you may be able to achieve this in a more structured way by use the 'Select Case' statement instead of all these If's
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    65

    Re: [2005] if...else statements. please help.

    oh, ok.

    well, what im making a poker game. i used a number generator, where each number from the selcted string goes into the textboxes. each number has a picture assigned to it.

    so with the code i posted. what it is meant to do is that it cycles through the the 1st if statement, if its nt true, then carry onto the next statement...and so on until it reaches a statement that is true. and when it does, i want it to end going throught all the if statements.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    65

    Re: [2005] if...else statements. please help.

    hey shaggy hiker. when u say add a vraible boolean and 'Return', do you mean this:

    Code:
            If (TextBox1.Text = "1" Or TextBox1.Text = "2" Or TextBox1.Text = "3") Xor _
            (TextBox2.Text = "1" Or TextBox2.Text = "2" Or TextBox2.Text = "3") Xor _
            (TextBox3.Text = "1" Or TextBox3.Text = "2" Or TextBox3.Text = "3") Xor _
            (TextBox4.Text = "1" Or TextBox4.Text = "2" Or TextBox4.Text = "3") Xor _
            (TextBox5.Text = "1" Or TextBox5.Text = "2" Or TextBox5.Text = "3") = True Then
                MsgBox("flushplay")
                Return
            End If

  7. #7
    Member
    Join Date
    Apr 2005
    Posts
    58

    Re: [2005] if...else statements. please help.

    Exit Sub inside each IF statement OR

    IF 'something' THEN
    'do something
    ELSEIF 'something a little different' THEN
    'do another thing
    ELSEIF 'something a little different' THEN
    'do another little something something
    END IF

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] if...else statements. please help.

    By the end of my post, I was favoring what Eclipsyo said, however, that Return would indeed work if exiting the sub that contains the If statement is an appropriate action at that time.
    My usual boring signature: Nothing

  9. #9
    Member
    Join Date
    Apr 2005
    Posts
    58

    Re: [2005] if...else statements. please help.

    Also, if your wanting to check the 1st IF then use a boolean to determine if you want to do another thing..
    Code:
    Dim blnFound as boolean = False
    
     If (TextBox1.Text = "1" Or TextBox1.Text = "2" Or TextBox1.Text = "3") Xor _
            (TextBox2.Text = "1" Or TextBox2.Text = "2" Or TextBox2.Text = "3") Xor _
            (TextBox3.Text = "1" Or TextBox3.Text = "2" Or TextBox3.Text = "3") Xor _
            (TextBox4.Text = "1" Or TextBox4.Text = "2" Or TextBox4.Text = "3") Xor _
            (TextBox5.Text = "1" Or TextBox5.Text = "2" Or TextBox5.Text = "3") = True Then
               blnFound = true
            End If
    
    'now use the blnFound variable to do something else....
    
    IF blnFound THEN 'means if blnFound = True
    
    
            If (TextBox1.Text = "1" Or TextBox1.Text = "2" Or TextBox1.Text = "3") Xor _
            (TextBox2.Text = "1" Or TextBox2.Text = "2" Or TextBox2.Text = "3") Xor _
            (TextBox3.Text = "1" Or TextBox3.Text = "2" Or TextBox3.Text = "3") Xor _
            (TextBox4.Text = "1" Or TextBox4.Text = "2" Or TextBox4.Text = "3") Xor _
            (TextBox5.Text = "1" Or TextBox5.Text = "2" Or TextBox5.Text = "3") Then
                MsgBox("flushplay")
            End If
    
    ELSEIF not blnFound   'means if blnFound = False
    
            If (TextBox1.Text = "1" Or TextBox1.Text = "2" Or TextBox1.Text = "3") Xor _
            (TextBox2.Text = "1" Or TextBox2.Text = "2" Or TextBox2.Text = "3") Xor _
            (TextBox3.Text = "1" Or TextBox3.Text = "2" Or TextBox3.Text = "3") Xor _
            (TextBox9.Text = "1" Or TextBox9.Text = "2" Or TextBox9.Text = "3") Xor _
            (TextBox8.Text = "1" Or TextBox8.Text = "2" Or TextBox8.Text = "3") Then
                MsgBox("flushcomp")
            End If
    END IF
    Just some explains of how to use a boolean as a determination of what to do next.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    65

    Re: [2005] if...else statements. please help.

    i now have the following code:

    Code:
            If (TextBox1.Text = "1" Or TextBox1.Text = "2" Or TextBox1.Text = "3") Xor _
            (TextBox2.Text = "1" Or TextBox2.Text = "2" Or TextBox2.Text = "3") Xor _
            (TextBox3.Text = "1" Or TextBox3.Text = "2" Or TextBox3.Text = "3") Xor _
            (TextBox4.Text = "1" Or TextBox4.Text = "2" Or TextBox4.Text = "3") Xor _
            (TextBox5.Text = "1" Or TextBox5.Text = "2" Or TextBox5.Text = "3") = True Then
                MsgBox("flushplay")
                Return
            End If
    
            If (TextBox1.Text = "1" Or TextBox1.Text = "2" Or TextBox1.Text = "3") Xor _
            (TextBox2.Text = "1" Or TextBox2.Text = "2" Or TextBox2.Text = "3") Xor _
            (TextBox3.Text = "1" Or TextBox3.Text = "2" Or TextBox3.Text = "3") Xor _
            (TextBox9.Text = "1" Or TextBox9.Text = "2" Or TextBox9.Text = "3") Xor _
            (TextBox8.Text = "1" Or TextBox8.Text = "2" Or TextBox8.Text = "3") = True Then
                MsgBox("flushcomp")
                Return
            End If
    
            If (TextBox1.Text = "4" Or TextBox1.Text = "5" Or TextBox1.Text = "6" Or TextBox1.Text = "7" Or TextBox1.Text = "8" Or TextBox1.Text = "9" Or TextBox1.Text = "10") Xor _
                    (TextBox2.Text = "4" Or TextBox2.Text = "5" Or TextBox2.Text = "6" Or TextBox2.Text = "7" Or TextBox2.Text = "8" Or TextBox2.Text = "9" Or TextBox2.Text = "10") Xor _
                    (TextBox3.Text = "4" Or TextBox3.Text = "5" Or TextBox3.Text = "6" Or TextBox3.Text = "7" Or TextBox3.Text = "8" Or TextBox3.Text = "9" Or TextBox3.Text = "10") Xor _
                    (TextBox4.Text = "4" Or TextBox4.Text = "5" Or TextBox4.Text = "6" Or TextBox4.Text = "7" Or TextBox4.Text = "8" Or TextBox4.Text = "9" Or TextBox4.Text = "10") Xor _
                    (TextBox5.Text = "4" Or TextBox5.Text = "5" Or TextBox5.Text = "6" Or TextBox5.Text = "7" Or TextBox5.Text = "8" Or TextBox5.Text = "9" Or TextBox5.Text = "10") = True Then
                MsgBox("highcardplay")
                Return
            End If
    
            If (TextBox1.Text = "4" Or TextBox1.Text = "5" Or TextBox1.Text = "6" Or TextBox1.Text = "7" Or TextBox1.Text = "8" Or TextBox1.Text = "9" Or TextBox1.Text = "10") Xor _
            (TextBox2.Text = "4" Or TextBox2.Text = "5" Or TextBox2.Text = "6" Or TextBox2.Text = "7" Or TextBox2.Text = "8" Or TextBox2.Text = "9" Or TextBox2.Text = "10") Xor _
            (TextBox3.Text = "4" Or TextBox3.Text = "5" Or TextBox3.Text = "6" Or TextBox3.Text = "7" Or TextBox3.Text = "8" Or TextBox3.Text = "9" Or TextBox3.Text = "10") Xor _
            (TextBox9.Text = "4" Or TextBox9.Text = "5" Or TextBox9.Text = "6" Or TextBox9.Text = "7" Or TextBox9.Text = "8" Or TextBox9.Text = "9" Or TextBox9.Text = "10") Xor _
            (TextBox8.Text = "4" Or TextBox8.Text = "5" Or TextBox8.Text = "6" Or TextBox8.Text = "7" Or TextBox8.Text = "8" Or TextBox8.Text = "9" Or TextBox8.Text = "10") = True Then
                MsgBox("highcardcomp")
                Return
            End If
    this does not work. eg, the second 'if 'statement was true, but the msgbox returns "flushplay" instead of "flushcomp"

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    65

    Re: [2005] if...else statements. please help.

    how do i use a select case method?

  12. #12
    Fanatic Member Clanguage's Avatar
    Join Date
    Jan 2008
    Location
    North Carolina
    Posts
    659

    Re: [2005] if...else statements. please help.

    It is real easy to use select case and it makes the code easy to read.
    Code:
              
    Select Case who
         Case Is = "Me"
               textbox1.text = "Hi Me"
          Case is = "you"
               textbox1.text = "Hi You"
          Case else
               textbox1.text = "Hi Everyone"
    End Select
    CLanguage;
    IF Post = HelpFull Then
    RateMe
    Else
    Say("Shut UP")
    End If
    DotNet rocks
    VB 6, VB.Net 2003, 2005, 2008, 2010, SQL 2005, WM 5.0,ahem ?OpenRoad?

  13. #13
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: [2005] if...else statements. please help.

    Quote Originally Posted by kapz20

    this does not work. eg, the second 'if 'statement was true, but the msgbox returns "flushplay" instead of "flushcomp"
    That would be because the first if statement was true as well. If that one is true, the second one will never get looked at.
    (VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    65

    Re: [2005] if...else statements. please help.

    if i take out " = true" from the statement, it still does the same. does "return" effect it in anyway?

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] if...else statements. please help.

    The = True part makes no difference, because the statement will be evaluated to True or False. Adding the = True part will simply evaluate the condition, and check to see if it is True, which will only happen if it already was True, so the check is redundant.

    The point is that with the code you showed, the only way the messagebox could be showing the wrong thing is if the first If statement was true. You said that the second one was true, but if the first one was true, the second one would never be reached, because the function would hit the Return statement in the first If statement and exit.
    My usual boring signature: Nothing

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