Results 1 to 7 of 7

Thread: Struggling with if...then and logical operator

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    41

    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.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  3. #3
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    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

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    41

    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.

  5. #5
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim a, b As Integer
    3.     Integer.TryParse(TextBox1.Text, a)
    4.     Integer.TryParse(TextBox2.Text, b)
    5.  
    6.     Dim oneIsSmall As Boolean = (a < 0 OrElse b < 0)
    7.     Dim oneIsBig As Boolean = (a > 1000 OrElse b > 1000)
    8.  
    9.     If oneIsSmall AndAlso oneIsBig Then
    10.         MessageBox.Show(String.Format("one is too small and one is too big : {0}, {1}", a, b))
    11.     ElseIf oneIsSmall Then
    12.         MessageBox.Show(String.Format("one or both are too small : {0}, {1}", a, b))
    13.     ElseIf oneIsBig Then
    14.         MessageBox.Show(String.Format("one or both are too big : {0}, {1}", a, b))
    15.     Else
    16.         MessageBox.Show(String.Format("both are within range : {0}, {1}", a, b))
    17.     End If
    18.  
    19. End Sub

  6. #6
    New Member
    Join Date
    Dec 2012
    Posts
    8

    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

  7. #7
    Junior Member
    Join Date
    Aug 2012
    Posts
    20

    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
  •  



Click Here to Expand Forum to Full Width