|
-
Feb 16th, 2013, 06:58 PM
#1
Thread Starter
Member
Struggling with if...then and logical operator
Hi,
I have two calculated values for which I wrote messages:
If value1 < 0 orelse value2 < 0 then
Message1.show
end if
If value1 > 1000 orelse value2 > 1000 then
Message2.show
end if
This works fine.
But it happens that one value is < 0 and the other > 1000 in the same time,
and the two messages pop up.
In this latter case I want a third Message to show up, overriding the two others.
I tried every possibility I know without success.
If you please help me.
-
Feb 16th, 2013, 07:07 PM
#2
Re: Struggling with if...then and logical operator
If value1 < 0 AndAlso value2 > 1000 OrElse value1 > 1000 AndAlso value2 < 0 Then
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Feb 16th, 2013, 07:21 PM
#3
Re: Struggling with if...then and logical operator
Instead of using two or more "If Then" statements you might want to try using one statement with ElseIf to break down the logic, example...
Code:
If value1 < 0 OrElse value2 < 0 Then
Debug.WriteLine("value1 and/or value2 are < 0")
ElseIf value1 > 1000 OrElse value2 > 1000 Then
Debug.WriteLine("value1 and/or value2 are > 1000")
Else
Debug.WriteLine("neither value1 or value2 are < 0 or > 1000")
End If
-
Feb 16th, 2013, 07:33 PM
#4
Thread Starter
Member
Re: Struggling with if...then and logical operator
Hi,
I truly thank you for responding quickly.
I tried what you suggested, but this doesn't work, unless I'm wrong somewhere in my small application.
-
Feb 16th, 2013, 09:11 PM
#5
Re: Struggling with if...then and logical operator
I think it is easier on the brain if you break it down further, so you are not mentally doing multiple comparisons every time you write an If clause.
Example using a Button and two TextBoxes:
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a, b As Integer
Integer.TryParse(TextBox1.Text, a)
Integer.TryParse(TextBox2.Text, b)
Dim oneIsSmall As Boolean = (a < 0 OrElse b < 0)
Dim oneIsBig As Boolean = (a > 1000 OrElse b > 1000)
If oneIsSmall AndAlso oneIsBig Then
MessageBox.Show(String.Format("one is too small and one is too big : {0}, {1}", a, b))
ElseIf oneIsSmall Then
MessageBox.Show(String.Format("one or both are too small : {0}, {1}", a, b))
ElseIf oneIsBig Then
MessageBox.Show(String.Format("one or both are too big : {0}, {1}", a, b))
Else
MessageBox.Show(String.Format("both are within range : {0}, {1}", a, b))
End If
End Sub
-
Feb 17th, 2013, 01:05 AM
#6
New Member
Re: Struggling with if...then and logical operator
Maybe you will laugh at me but what about this:
If math.max(val1,val2) > 1000 and min(val1,val2) > 0 then msg1
If math.max(val1,val2) < 1000 and min(val1,val2) < 0 then msg2
If math.max(val1,val2) > 1000 and min(val1,val2) < 0 then msg3
-
Feb 18th, 2013, 05:19 PM
#7
Junior Member
Re: Struggling with if...then and logical operator
Hi, I truly thank you for your help. I found what was wrong in my simple application.
I truly thank you and thanks to any other person offering help.
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
|