Results 1 to 15 of 15

Thread: [2005] Need help with IF statment

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    910

    [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!

  2. #2
    Lively Member
    Join Date
    Sep 2007
    Location
    Texas
    Posts
    98

    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

  3. #3
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [2005] Need help with IF statment

    vb Code:
    1. If Variable1 = True AndAlso (Variable2 = True OrElse Variable3 = True) Then
    2.   'Code here
    3. End If

    The parenthesis around (Variable2 = True OrElse Variable3 = True) is what makes the difference
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. If Variable1 AndAlso (Variable2 OrElse Variable3) Then
    2.   'Code here
    3. End If
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    910

    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!

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    910

    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?

  7. #7
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: [2005] Need help with IF statment

    Quote 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...

    Quote 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.

  8. #8
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: [2005] Need help with IF statment

    Quote 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.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    910

    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

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  11. #11
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    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.

  12. #12
    Junior Member
    Join Date
    Apr 2007
    Location
    Singapore
    Posts
    22

    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

  13. #13
    Addicted Member
    Join Date
    Nov 2006
    Location
    Minnesota
    Posts
    235

    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

  14. #14
    Junior Member
    Join Date
    Apr 2007
    Location
    Singapore
    Posts
    22

    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"

  15. #15
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [2005] Need help with IF statment

    Quote 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
  •  



Click Here to Expand Forum to Full Width