dcsimg
Results 1 to 9 of 9
  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Resolved [RESOLVED] If Not Statement for second statement doesnt work

    I want to combine following two codes.

    Code:
    If Not 11 < 10 Then
      MessageBox.Show("Hello")
    End If
    Code:
    If Not 22 < 20 Then
      MessageBox.Show("Hello")
    End If
    Why If Not doesnt work for second condition?

    Code:
    If Not 11 < 10 And 22 < 20 Then
      MessageBox.Show("Hello")
    End If

    P.S.= Using If Not is mandatory. So, please dont advice me other solutions.
    Last edited by Kram Kramer; Jun 13th, 2018 at 10:32 AM.

  2. #2
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    335

    Re: If Not Statement for second statement doesnt work

    Because you aren't properly combining the two If statements. The inclusion of Not in front only applies to the 11 < 10 comparison, the 22 < 20 is evaluated exactly as-is, without the Not. So it turns into:

    If Not False And False, which results in False. So, you would need to do something like this:

    Code:
    If Not 11 < 10 And Not 22 < 20 Then

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,695

    Re: If Not Statement for second statement doesnt work

    both of these methods should work...

    Code:
    If Not (11 < 10 And 22 < 20) Then
      MessageBox.Show("Hello")
    End If
    Code:
    If Not 11 < 10 And Not 22 < 20 Then
      MessageBox.Show("Hello")
    End If

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: If Not Statement for second statement doesnt work

    Thanks a lot OptionBase1. Your answer is exact answer which I am looking for.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: If Not Statement for second statement doesnt work

    Thanks a lot paul. Your answer is exact answer which I am looking for.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,898

    Re: [RESOLVED] If Not Statement for second statement doesnt work

    It won't make a difference to the end result in this case but that code should also be using AndAlso rather than And. AndAlso and OrElse should be the default options because they short-circuit and the And and Or operators should only be used when you specifically don't want short-circuiting, which should be very rare.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: [RESOLVED] If Not Statement for second statement doesnt work

    @jmcilhinney Do you have another solution?

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,695

    Re: [RESOLVED] If Not Statement for second statement doesnt work

    Quote Originally Posted by Kram Kramer View Post
    @jmcilhinney Do you have another solution?
    It works...

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,898

    Re: [RESOLVED] If Not Statement for second statement doesnt work

    Quote Originally Posted by Kram Kramer View Post
    @jmcilhinney Do you have another solution?
    What's wrong with the two options you already have?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.