[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
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. :)
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.
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
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.
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
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
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.
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.
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"
Re: [2005] if...else statements. please help.
how do i use a select case method?
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
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.
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?
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.