|
-
Dec 4th, 2007, 04:11 PM
#1
Thread Starter
Fanatic Member
[2005] Need help with IF statment
Hi,
I am trying to create an if statement with three things that must be check. I need VARIABLE1 to be true and need VARIABLE2 OR VARIABLE3 to be true also. I just can't seem to get the logic right. If I do VARIABLE1 = True AND VARIABLE2 = True OR VARIABLE3 = True then obviously if VARIABLE3 is true then it will pass regardless if VARIABLE1 is true or now. And I can't think of any other way. Any ideas?
Thanks!
-
Dec 4th, 2007, 04:19 PM
#2
Lively Member
Re: [2005] Need help with IF statment
Code:
if Variable1 = true then
if variable2 = true or variable3 = true then
here goes your code
end if
end if
hpoe this helps
-
Dec 4th, 2007, 04:47 PM
#3
Re: [2005] Need help with IF statment
vb Code:
If Variable1 = True AndAlso (Variable2 = True OrElse Variable3 = True) Then
'Code here
End If
The parenthesis around (Variable2 = True OrElse Variable3 = True) is what makes the difference
-
Dec 4th, 2007, 04:56 PM
#4
Re: [2005] Need help with IF statment
You dont actually need to compare the boolean variable with the boolean constant True. This is sufficient:
VB.Net Code:
If Variable1 AndAlso (Variable2 OrElse Variable3) Then
'Code here
End If
-
Dec 5th, 2007, 08:44 AM
#5
Thread Starter
Fanatic Member
Re: [2005] Need help with IF statment
Sunshine, your code would work and I don't know why I didn't think about doing it like that. I am going to use JuggaloBrotha's suggestion since it combines it into one line.
Atheist, It actually compares it to text, I just did it like that because it was easier to explain.
Thanks guys!
-
Dec 5th, 2007, 09:08 AM
#6
Thread Starter
Fanatic Member
Re: [2005] Need help with IF statment
For some reason this wouldn't work. I am using text instead of "true". And actually I am checking variable2 to be equal to two different texts. There really is no variable3. Here's my code.
Code:
If Variable1 = "Text1" AndAlso (Variable2 = "Text2" OrElse Variable2 = "Text3") Then
For some reason this code will not continue if my Variable2 is "Text2" or "Text3". Anyone see why?
-
Dec 5th, 2007, 09:48 AM
#7
Fanatic Member
Re: [2005] Need help with IF statment
 Originally Posted by jre1229
Hi,
I am trying to create an if statement with three things that must be check. I need VARIABLE1 to be true and need VARIABLE2 OR VARIABLE3 to be true also. I just can't seem to get the logic right. If I do VARIABLE1 = True AND VARIABLE2 = True OR VARIABLE3 = True then obviously if VARIABLE3 is true then it will pass regardless if VARIABLE1 is true or now. And I can't think of any other way. Any ideas?
Thanks!
Im still not sure what your trying to achieve...
 Originally Posted by jre1229
hen obviously if VARIABLE3 is true then it will pass regardless if VARIABLE1 is true or now
if any of them is true then it will pass?
it works 60% of the time, all the time.
-
Dec 5th, 2007, 09:59 AM
#8
Fanatic Member
Re: [2005] Need help with IF statment
 Originally Posted by jre1229
For some reason this wouldn't work. I am using text instead of "true". And actually I am checking variable2 to be equal to two different texts. There really is no variable3. Here's my code.
Code:
If Variable1 = "Text1" AndAlso (Variable2 = "Text2" OrElse Variable2 = "Text3") Then
For some reason this code will not continue if my Variable2 is "Text2" or "Text3". Anyone see why?
That code works fine
Ive tested it..
are you sure your setting your variables
If Me.TextBox1.Text = "1" AndAlso (Me.TextBox2.Text = "2" OrElse Me.TextBox2.Text = "3") Then
MessageBox.Show("PASSED")
Else
MessageBox.Show("fAILED")
End If
it works 60% of the time, all the time.
-
Dec 5th, 2007, 11:57 AM
#9
Thread Starter
Fanatic Member
Re: [2005] Need help with IF statment
Could it have to do with me using <> to say it is not equal?
Code:
If Variable1 = "Text1" AndAlso (Variable2 <> "Text2" OrElse Variable2 <> "Text3") Then
-
Dec 5th, 2007, 03:07 PM
#10
Re: [2005] Need help with IF statment
The problem appears to be with AND.
The way you have the code, both of these conditions MUST be true:
1) Variable1 is Text1
2) Either Variable2 = Text2 or Text3
That agrees with the way you described the problem in the first post, but then in post #6, you suggest that you want something somewhat different.
If you use <> in place of = within the parenthesis, you have a totally different issue. That would be saying "Variable 2 is NOT string2 or NOT string 3". Well...that's true no matter what variable 2 is, because if variable 2 is string2 then it is NOT string 3, and if it is string3 then it certainly can't be string2.
My usual boring signature: Nothing
 
-
Dec 6th, 2007, 03:42 AM
#11
Fanatic Member
Re: [2005] Need help with IF statment
I think you need to tell us exactly what you want and im sure someone here can show you
Post 9 is the first time ive seen the not equals (<>)
post 2 by sinshinebh should work for you if you do it all in nested ifs
if var1 = "this" then
if var2 <> "this then
if var3 = "this" then
' its true
thats if thats what your looking for - it may be clearer if you try it that way
it works 60% of the time, all the time.
-
Dec 6th, 2007, 07:43 AM
#12
Junior Member
Re: [2005] Need help with IF statment
If (Variable1 = "Text1") Then
If (Variable2 = "Text2") Then
<code if its text2>
Else
If (Variable2 = "Text3") Then
<code if its text3>
End If
End If
End If
"There are a thousand ways to a solution in coding. Sometimes, you have to sacrifice size for functionality." - Unknown
-
Dec 6th, 2007, 02:15 PM
#13
Addicted Member
Re: [2005] Need help with IF statment
As I understand your posts, I think you might be looking for
Code:
If Variable1="Text1" AndAlso Not (Variable2="Text2" Or Variable2="Text3") Then
'Code here
End If
-
Dec 13th, 2007, 10:52 AM
#14
Junior Member
Re: [2005] Need help with IF statment
the amount of english in vb.net...
"Work, as if you don't need money
Love, as if nobody has hurt you
Dance, as if nobody was watching you
Sing, as if nobody was listening
Live, as if this was paradise on earth"
-
Dec 13th, 2007, 10:56 AM
#15
Re: [2005] Need help with IF statment
 Originally Posted by Whosat
the amount of english in vb.net...
What does this mean?
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
|