Results 1 to 27 of 27

Thread: if statement

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    if statement

    Is there a way to make sure something is not two things within one if statement?

    I don't want to write out two different if statements, but will if needed. I want to cut down code, though.

    I want something like:

    VB Code:
    1. If var <> ("Text1" Or "Text2") Then

    I'm just not sure if this is possible or not.
    Like Archer? Check out some Sterling Archer quotes.

  2. #2
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    Are you asking if "or" is valid?

    If so, yes.. and so is "and"

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    uhh.. no.

    I'm not really a beginner..

    I don't know how I could make it clearer, I'm asking if I can put two conditions into the same if statement, but I don't want to do have to do the norm, IE:

    VB Code:
    1. If var <> "Text1" Or var <> "Text2" Then

    I don't want to enter the variable twice.
    Like Archer? Check out some Sterling Archer quotes.

  4. #4
    Addicted Member
    Join Date
    Jan 2003
    Posts
    163
    if(var="Text1" or var="Text2") then

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    ok, look.. I already know that can be done, I showed that I knew that in my second post. I don't want to enter the variable name twice, I just want to check it's contents within one if statement, like I showed in my first post with the code I made up..

    Anyone even understand what I want, or no?
    Like Archer? Check out some Sterling Archer quotes.

  6. #6
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    Why not just create a new project and try it?

    Then come back and read my first answer.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    I already have tried the code I made.. why would I come ask for help if I already had the code?

    I just made a new project and tried it anyway..

    VB Code:
    1. Dim var1 As String, var2 As String, var3 As String
    2. Private Sub Form_Load()
    3.   var1 = "BLAH"
    4.   var2 = "BLAH2"
    5. End Sub
    6. Private Sub Text1_Change()
    7.   var3 = Text1.Text
    8.   If var3 = (var1 Or var2) Then
    9.     MsgBox "!!"
    10.   End If
    11. End Sub

    Try it yourself.
    Like Archer? Check out some Sterling Archer quotes.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    I've always wanted to do that, too, but in most cases it doesn't really make logical sense. We talk that way, but it is just a sort of shorthand representation in speech.

    When you say "A is greater than B or C", you mean "A is greater than B, and A is greater than C". We understand what you mean, but the statement is a form of logical concatenation. Of course, in some situations, you could make a program do that, but it takes a contortion that can be expensive.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    from these guys' replies, they didn't get what I meant at all..
    Like Archer? Check out some Sterling Archer quotes.

  10. #10
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    kows


    You can do that, but it will not have the same meaning logically.

    What you want:
    Example values for A = 1, B = 1, C = 2

    if A <> B and A <> C gives you

    Is 1 <> 1 and 1 <> 2

    What the orther statement actually give you
    Is A <> (B or C)

    if A <> (1 or 2)

    In which the value in parenthisis is actually equated to 2. so you get (representing a logical OR).

    if A <> (2)

    This is why you cannot do what you want.

  11. #11
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Hey guys, don't get heated! Communication is always one of the big problems in Forums.


    Kows, I think that one of the reasons for the misunderstanding is that you are asking for something that is just not available in VB Computors are not as intelligent as (most) humans. They can only understand very simple steps. Also when you say "I don't want to write out two different if statements, " the following code which was quoted to you by john24 IS only one statement.

    "if(var="Text1" or var="Text2") then"


    and it works with or without the brackets. You HAVE to clearly state the conditions you are applying. You cannot miss out words (in this case the name of the object) just because you, as a human, know what you mean - the computor doesn't. On the other hand, the computor, being unable to think, carries out the statement far quicker that you can, so it is not necessary to take short cuts in the language. If you are not happy with this, then you will just have to invent your own computor language!! That will take you about 10 million man-hours!
    Last edited by taxes; Feb 7th, 2004 at 04:35 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  12. #12
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi Randem,

    "You can do that, but it will not have the same meaning logically."

    He CAN'T do that!!!!!!!

    As for the rest of your post, does anyone else have the slightest idea of what you mean? I'm afraid I don't.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  13. #13
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi ShaggyHiker,

    "When you say "A is greater than B or C", you mean "A is greater than B, and A is greater than C". "

    NO he doesn't!

    He is stating a condition that A must be greater than B or C.
    A does not have to be greater than both B and C.

    e.g. if a =3, b=2, and c=5

    then A > B or C is true ( A is greater than B)

    but a> B and C is false ( A is greater than B but smaller than C).

    Oh boy. Some of you guys need to sit down with an icepack on your foreheads

    Don't take offence anyone, but you do have to read the questions carefully and not jump in with the first thing that comes into your head.
    Last edited by taxes; Feb 7th, 2004 at 04:33 PM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  14. #14
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    taxes



    If you don't have the slightest idea... why reply? And he can do that. It is not impossible, he will just get the wrong meaning most of the time.

    Read whole statements. You acuse others of what you do.

    And simple math would help also.

  15. #15
    Addicted Member imbue's Avatar
    Join Date
    Aug 2002
    Location
    Midwest USA
    Posts
    155
    Kows, the short answer to your question is no.

    But if all you want to do is shorten down your code then you could write a function to do that. Then you can just call the function like:

    VB Code:
    1. If twoIFs(var, "Text1", "Text2") then

    Of course that will slow down your project if you're calling it thousands of times a second.

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    never actually thought about a function imbue, think I'll try that and see how it works out for now.

    oh, also, taxes.. I'm not programming in VB.NET, so it wouldn't matter if it was available in it or not. I know what a programming language is and I also know how it works, so there's no need for you to 'explain' it. It would be nice to have something like that built into a future version of VB, though.
    Last edited by kows; Feb 7th, 2004 at 01:35 AM.
    Like Archer? Check out some Sterling Archer quotes.

  17. #17
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi Kows,

    "oh, also, taxes.. I'm not programming in VB.NET, so it wouldn't matter if it was available in it "


    My typing error, I should have said "not available in VB".
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  18. #18
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    703
    Curse VB's excessive operator overloading. It overloads ( ), =, and the or/and operators a tad too much IMO. Having separate operators for bitwise and for logical or'ing would be much clearer.
    an ending

  19. #19
    Addicted Member
    Join Date
    Apr 2003
    Posts
    170
    sorry that i read only the first few replies (in case
    my answer seems irrelavent)

    what you want is not possible......... !!exactly!!,
    but i have a way of doing what you want in
    one logical test, but the result will not
    reflect what you want 100% but maybe about 99%.

    instead of
    VB Code:
    1. If var<>"text1" Or var<>"text2" Then
    use
    VB Code:
    1. If Not "text1text2" Like "*" & var & "*" Then
    im sure i once heard of a IsIn function, so that
    "me" IsIn "kemer" ... returns true, im not sure
    which language supports this test,
    check out if its in vb, this might be used instead of like statement.

  20. #20
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi Randem,

    I'm sorry, but I HAVE read your post carefully - several times but I just cannot make any sense out of your comments:

    "What the orther statement actually give you
    Is A <> (B or C)
    if A <> (1 or 2)"

    (I understand those bits but not the following)

    "In which the value in parenthisis is actually equated to 2. so you get (representing a logical OR).

    if A <> (2)"
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  21. #21
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi ZaidGS

    "im sure i once heard of a IsIn function, so that
    "me" IsIn "kemer" ... returns true, im not sure
    which language supports this test, "

    Are you thinking of the InStr function of VB6 and VB.NET?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  22. #22
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    taxes,

    I made a slight mistake,

    A <> (B or C) - Represented by A=1, B=1, C=2 then would be

    1 <> (1 or 2) - When you logically or a 1 and a 2 you get 3 so this equates to:

    1 <> (3) - So 1 <> 3 is true, but it's not exactly what is wanted.


    Logically

    1 or 2 = 3
    1 or 3 = 3
    1 or 4 = 5
    1 or 5 = 5

    2 or 3 = 3
    2 or 4 = 6

    so on an so on.

  23. #23
    Addicted Member
    Join Date
    Jan 2004
    Posts
    162

    Wink

    Yo!

    I don't know if this has been resolved yet, but using the code that kows posted the third time, with a little change, it should work the way he wants it too:
    VB Code:
    1. Option Explicit
    2. Dim var1 As String, var2 As String, var3 As String
    3. Private Sub Form_Load()
    4.   var1 = "BLAH"
    5.   var2 = "BLAH2"
    6. End Sub
    7. Private Sub Text1_Change()
    8.   var3 = Text1.Text
    9.   If var3 = var1 Or var3 = var2 Then
    10.     MsgBox ("!!")
    11.   End If
    12. End Sub
    Anyway that worked for me!!!!!

    I think you'll have to use the var3 twice in the if statement. If i used:
    VB Code:
    1. If var3 = (var1 or var2) then
    I got an error: Type Mismatch
    but if i used:
    VB Code:
    1. If var3 = var1 or var3 = var2 then
    It worked fine. So i'm pretty sure you'll have to used the var3 twice, anyway, you're only using like 3 more words in the code
    Last edited by SuperChris9; Feb 8th, 2004 at 01:25 AM.

  24. #24
    Addicted Member
    Join Date
    Apr 2003
    Posts
    170
    Originally posted by taxes
    Hi ZaidGS

    "im sure i once heard of a IsIn function, so that
    "me" IsIn "kemer" ... returns true, im not sure
    which language supports this test, "

    Are you thinking of the InStr function of VB6 and VB.NET?
    how does that work ?!?!

  25. #25
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi ZaidGS,

    From MSDN Help Index Instr function.


    InStr Function


    Returns a Variant (Long) specifying the position of the first occurrence of one string within another.

    Syntax

    InStr([start, ]string1, string2[, compare])

    The InStr function syntax has thesearguments:

    Part Description
    start Optional.Numeric expression that sets the starting position for each search. If omitted, search begins at the first character position. If start containsNull, an error occurs. The start argument is required if compare is specified.
    string1 Required.String expression being searched.
    string2 Required. String expression sought.
    compare Optional. Specifies the type ofstring comparison. If compare is Null, an error occurs. If compare is omitted, the Option Compare setting determines the type of comparison.


    Settings

    The compare argument settings are:

    Constant Value Description
    vbUseCompareOption -1 Performs a comparison using the setting of the Option Compare statement.
    vbBinaryCompare 0 Performs a binary comparison.
    vbTextCompare 1 Performs a textual comparison.
    vbDatabaseCompare 2 Microsoft Access only. Performs a comparison based on information in your database.


    Return Values

    If InStr returns
    string1 is zero-length 0
    string1 is Null Null
    string2 is zero-length start
    string2 is Null Null
    string2 is not found 0
    string2 is found within string1 Position at which match is found
    start > string2 0


    Remarks

    The InStrB function is used with byte data contained in a string. Instead of returning the character position of the first occurrence of one string within another, InStrB returns the byte position.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  26. #26
    Addicted Member
    Join Date
    Jul 2000
    Location
    FeedMeTown, Ohio
    Posts
    176
    For the record...

    Dim ImChubbyToo as Boolean, MyChubbyUncle as Boolean, MyChubbyAunt as Booean

    MyChubbyAunt = True
    MyChubbyUncle = True

    This latter will out perform the following...

    'Comment: must evaluate both clauses
    If (MyChubbyAunt and MyChubbyUncle) then
    ImChubbyToo = True
    End If

    'Comment: must evaluate first clause only... if true, then evaluate second clause = Faster Code
    If MyChubbyAunt then
    If MyChubbyUncle then
    ImChubbyToo = True
    End If
    End If

    So.. to sum up... what you're doing is bad practice. Evaluate variables only as required.
    Last edited by EZapps; Feb 10th, 2004 at 10:15 PM.

  27. #27
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi Ezaps,

    Yes, this works with Select Case also BUT:

    "If (MyChubbyAunt and MyChubbyUncle) then"


    really is

    "If (MyChubbyAunt = True and MyChubbyUncle=True) then"

    so that it does not achieve what kows wants.

    However, I agree with your final statement.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

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