Results 1 to 23 of 23

Thread: [RESOLVED] Conditional Logic In One Line

  1. #1

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Resolved [RESOLVED] Conditional Logic In One Line

    I need to compare the data between two text boxes and a variable. If the value of the boxes don't match up and the variable is -1, I need to fail it.

    When I do this:

    VB.NET Code:
    1. If Not Me.txtPcidSystem.Text = Me.txtPcidDatabase.Text Then

    I have no troubles, but adding the variable check produces a false positive.

    VB.NET Code:
    1. If Not Me.txtPcidSystem.Text = Me.txtPcidDatabase.Text And TestData.BypassCheck_PCID = -1 Then

    The way the code is laid out, I have to do this in one line. I can't check the first condition on one line and the second on another.

    I've tried separating the data out with parantheses, but it's producing the same results.

    Any ideas what I'm doing wrong here?
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Conditional Logic In One Line

    See if this will work

    Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim BypassCheck_PCID As Integer = -1
    
        txtPcidSystem.Text = "Kevin"
        txtPcidDatabase.Text = "Kevin"
        Dim Result = If(txtPcidSystem.Text.Equals(txtPcidDatabase.Text) And BypassCheck_PCID = -1, "Yes", "No")
        Console.WriteLine(Result)
    
        BypassCheck_PCID = 0
        Result = If(txtPcidSystem.Text.Equals(txtPcidDatabase.Text) And BypassCheck_PCID = -1, "Yes", "No")
        Console.WriteLine(Result)
    
        BypassCheck_PCID = -1
        Result = If(txtPcidSystem.Text.Equals(txtPcidDatabase.Text) And BypassCheck_PCID = -1, "Yes", "No")
        Console.WriteLine(Result)
    
        txtPcidDatabase.Text = "Bob"
        Result = If(txtPcidSystem.Text.Equals(txtPcidDatabase.Text) And BypassCheck_PCID = -1, "Yes", "No")
        Console.WriteLine(Result)
    End Sub
    Results
    Code:
    Yes
    No
    Yes
    No

  3. #3
    Addicted Member vb_ftw's Avatar
    Join Date
    Dec 2010
    Posts
    139

    Re: Conditional Logic In One Line

    i would do it like this:

    vb Code:
    1. If Not (Me.txtPcidSystem.Text = Me.txtPcidDatabase.Text And TestData.BypassCheck_PCID = -1) Then

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Conditional Logic In One Line

    Edited: I originally misread the question

    You should be able to do it like this
    Code:
    If Me.txtPcidSystem.Text <> Me.txtPcidDatabase.Text And TestData.BypassCheck_PCID = -1 Then
    Last edited by DataMiser; Feb 27th, 2012 at 05:58 PM.

  5. #5

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: Conditional Logic In One Line

    Quote Originally Posted by kevininstructor View Post
    See if this will work

    Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim BypassCheck_PCID As Integer = -1
    
        txtPcidSystem.Text = "Kevin"
        txtPcidDatabase.Text = "Kevin"
        Dim Result = If(txtPcidSystem.Text.Equals(txtPcidDatabase.Text) And BypassCheck_PCID = -1, "Yes", "No")
        Console.WriteLine(Result)
    
        BypassCheck_PCID = 0
        Result = If(txtPcidSystem.Text.Equals(txtPcidDatabase.Text) And BypassCheck_PCID = -1, "Yes", "No")
        Console.WriteLine(Result)
    
        BypassCheck_PCID = -1
        Result = If(txtPcidSystem.Text.Equals(txtPcidDatabase.Text) And BypassCheck_PCID = -1, "Yes", "No")
        Console.WriteLine(Result)
    
        txtPcidDatabase.Text = "Bob"
        Result = If(txtPcidSystem.Text.Equals(txtPcidDatabase.Text) And BypassCheck_PCID = -1, "Yes", "No")
        Console.WriteLine(Result)
    End Sub
    Results
    Code:
    Yes
    No
    Yes
    No
    The issue with this is the same as my original problem. I need to be able to return a negative result and all I get are positives here. Even when trying the last example, I still get a false positive.

    Quote Originally Posted by vb_ftw View Post
    i would do it like this:

    vb Code:
    1. If Not (Me.txtPcidSystem.Text = Me.txtPcidDatabase.Text And TestData.BypassCheck_PCID = -1) Then
    Works perfectly. I had my parentheses mixed up.

    Quote Originally Posted by DataMiser View Post
    Edited: I originally misread the question

    You should be able to do it like this
    Code:
    If Me.txtPcidSystem.Text <> Me.txtPcidDatabase.Text And TestData.BypassCheck_PCID <> -1 Then
    This also works. Thanks
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Conditional Logic In One Line

    Seems I did as well as DataMiser but simple to fix
    Code:
    Dim Result = If((Not txtPcidSystem.Text.Equals(txtPcidDatabase.Text)) And BypassCheck_PCID = -1, "Yes", "No")

  7. #7

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: Conditional Logic In One Line

    Quote Originally Posted by kevininstructor View Post
    Seems I did as well as DataMiser but simple to fix
    Code:
    Dim Result = If((Not txtPcidSystem.Text.Equals(txtPcidDatabase.Text)) And BypassCheck_PCID = -1, "Yes", "No")
    Actually, this revision is the only one that works. The others always produce a negative result.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  8. #8
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: [RESOLVED] Conditional Logic In One Line

    Quote Originally Posted by weirddemon View Post
    The way the code is laid out, I have to do this in one line. I can't check the first condition on one line and the second on another.
    I don't understand this. Could you explain please?

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] Conditional Logic In One Line

    The way the code is laid out, I have to do this in one line. I can't check the first condition on one line and the second on another.
    hmm I did not even notice that comment in the OP. It would be simple to do it on more than one line.
    Code:
    If Not Me.txtPcidSystem.Text = Me.txtPcidDatabase.Text then
        If TestData.BypassCheck_PCID = -1 Then
             'this line will execute only if the text in the first test does not match and the second test =-1
        Else
             'this line will execute only if the text in the first test does not match and the second is not = -1
        End If
    Else
        'This line will execute only if the text matches
    End If

  10. #10
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: [RESOLVED] Conditional Logic In One Line

    Actually DataMiser, your code has subtly different behaviour. Note that the OP has the two conditions joined with And, not AndAlso. If reading the BypassCheck_PCID property had any side effect, your code wouldn't trigger it if the texts didn't match, whereas it is always triggered in the OP. What is a more correct translation is to evaluate both expressions and store in two boolean variables, then do the If blocks on those variables.

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] Conditional Logic In One Line

    That is true, I did not consider that there may be some side effect in testing the bypass.

  12. #12

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: [RESOLVED] Conditional Logic In One Line

    Quote Originally Posted by Evil_Giraffe View Post
    I don't understand this. Could you explain please?
    Quote Originally Posted by DataMiser View Post
    hmm I did not even notice that comment in the OP. It would be simple to do it on more than one line.
    I must do it on one line because of how the code flows together. It's one big block of conditional checks to ensure each step is done correctly.

    So it first checks if Item A is correct. If not, it fails. End of block. If it passes, it goes to item B and so on. If It fails on one line, the entire block ends. If I do another conditional check within the fail block, I have no way of continuing the logic without a GoTo (which I'll never use GoTos) or putting all of the code in subs or something, which isn't feasible.

    In this case, the text has to match up and the bypass has to be -1 to pass.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  13. #13
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: [RESOLVED] Conditional Logic In One Line

    There is no reason why this should not work, AndAlso is OK in this case according to the OP description of the problem. If the first part, that the two textboxes are different, there is no need to check for the variable.

    vb Code:
    1. If (Me.txtPcidSystem.Text <> Me.txtPcidDatabase.Text) AndAlso (TestData.BypassCheck_PCID = -1) Then Fail
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  14. #14

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: [RESOLVED] Conditional Logic In One Line

    Quote Originally Posted by kaliman79912 View Post
    There is no reason why this should not work, AndAlso is OK in this case according to the OP description of the problem. If the first part, that the two textboxes are different, there is no need to check for the variable.

    vb Code:
    1. If (Me.txtPcidSystem.Text <> Me.txtPcidDatabase.Text) AndAlso (TestData.BypassCheck_PCID = -1) Then Fail
    No. AndAlso won't work. The logic depends on all parts being correct. Not only does the text have to match up, but the bypass check must equal -1. This is all mostly educational discussion as this point though because kevininstructor's example works perfectly.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  15. #15
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] Conditional Logic In One Line

    never mind

    I have been getting confused in this thread myself.
    Last edited by DataMiser; Feb 28th, 2012 at 05:06 PM.

  16. #16

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: [RESOLVED] Conditional Logic In One Line

    Quote Originally Posted by DataMiser View Post
    ANDAlso should work just as well.

    The difference between this and AND is that the andalso will not bother with the second test if the first one fails which should not be an issue since you require both to pass.
    I know what AndAlso does. I was getting the logic mixed up in my head. But your approach is messier than kevin's. So although it might work, it's not ideal.
    Last edited by weirddemon; Feb 28th, 2012 at 05:12 PM.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  17. #17
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: [RESOLVED] Conditional Logic In One Line

    Quote Originally Posted by weirddemon View Post
    No. AndAlso won't work. The logic depends on all parts being correct. Not only does the text have to match up, but the bypass check must equal -1. This is all mostly educational discussion as this point though because kevininstructor's example works perfectly.
    The way AndAlso works is that it checks for the first condition, and If that condition is true, then it checks the second one. So in fact it will only trigger when BOTH are true, so it works. The difference from just And is that if the first part is false, then the second part is not checked as it is not necesary since, as you said, both need to be true. And checks both parts regardless.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  18. #18
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: [RESOLVED] Conditional Logic In One Line

    Quote Originally Posted by weirddemon View Post
    I must do it on one line because of how the code flows together. It's one big block of conditional checks to ensure each step is done correctly.

    So it first checks if Item A is correct. If not, it fails. End of block. If it passes, it goes to item B and so on. If It fails on one line, the entire block ends. If I do another conditional check within the fail block, I have no way of continuing the logic without a GoTo (which I'll never use GoTos) or putting all of the code in subs or something, which isn't feasible.

    In this case, the text has to match up and the bypass has to be -1 to pass.
    This description makes me think that there is a missing abstraction in your code involving these "steps". I think there is potential for making your overall code considerably shorter, easier to understand and considerably easier to maintain and extend, if you're interested in exploring that.

    [Edit: Also (ha!), I think we are all actually agreeing on the way AndAlso works. WD has already said he was getting the logic mixed up in his head. Let's all leave that non-argument alone, shall we?]

  19. #19
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: [RESOLVED] Conditional Logic In One Line

    Quote Originally Posted by Evil_Giraffe View Post
    [Edit: Also (ha!), I think we are all actually agreeing on the way AndAlso works. WD has already said he was getting the logic mixed up in his head. Let's all leave that non-argument alone, shall we?]
    Agree, in fact, Let's leave all argument alone since this Thread is [RESOLVED]
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  20. #20

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: [RESOLVED] Conditional Logic In One Line

    Quote Originally Posted by kaliman79912 View Post
    The way AndAlso works is that it checks for the first condition, and If that condition is true, then it checks the second one. So in fact it will only trigger when BOTH are true, so it works. The difference from just And is that if the first part is false, then the second part is not checked as it is not necesary since, as you said, both need to be true. And checks both parts regardless.
    I told you already. I know how AndAlso works. Your method is still worse than kevin's.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  21. #21

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: [RESOLVED] Conditional Logic In One Line

    Quote Originally Posted by Evil_Giraffe View Post
    This description makes me think that there is a missing abstraction in your code involving these "steps". I think there is potential for making your overall code considerably shorter, easier to understand and considerably easier to maintain and extend, if you're interested in exploring that.

    [Edit: Also (ha!), I think we are all actually agreeing on the way AndAlso works. WD has already said he was getting the logic mixed up in his head. Let's all leave that non-argument alone, shall we?]
    I tried to think about this as logically as possible when I made the code. It's fairly easy to extend, the only problem is that any conditional checks have to be made on one line.

    So let's say I have 10 items to validate. I'll check the first and then if it fails, the code completely stops. This is ideal. But if it fails and then I have to do another validation within that block, it won't continue because the rest of the logic is within the first Else block.

    The only other way, is to have everything in separate subs and call the appropriate sub each time. But that causes a lot of fragmentation. I like how it is now because everything follows a nice and simple order.

    But if you have a better way, I'd more than appreciate the ideas.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  22. #22
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: [RESOLVED] Conditional Logic In One Line

    Ah! I think I see what your scenario is. You've got something like the following?

    vbnet Code:
    1. If someLongAndConvolutedExpression1 AndAlso someLongAndConvolutedExpression2 Then
    2.     ' Do something
    3. Else
    4.     ' Do something else
    5. End If

    And you're trying to avoid the problem of duplicating the something else:
    vbnet Code:
    1. If someLongAndConvolutedExpression1 Then
    2.     If someLongAndConvolutedExpression2 Then
    3.         ' Do something
    4.     Else
    5.         ' Do something else
    6.     End If
    7. Else
    8.     ' Do something else
    9. End If

    With my revised understanding, I'd ask is this just in the one place that you need this pattern? If so, I probably wouldn't bother with building an abstraction, but consider that you can do this:

    vbnet Code:
    1. Dim condition1 As Boolean = someLongAndConvolutedExpression1
    2. Dim condition2 As Boolean = someLongAndConvolutedExpression2
    3. If condition1 AndAlso condition2 Then
    4.     ' Do something
    5. Else
    6.     ' Do something else
    7. End If

    Saves on all those parentheses and might be more readable:

    vbnet Code:
    1. Dim Result = If((Not txtPcidSystem.Text.Equals(txtPcidDatabase.Text)) And BypassCheck_PCID = -1, "Yes", "No")

    c.f.

    vbnet Code:
    1. Dim systemAndDatabaseAreDifferent As Boolean = Not txtPcidSystem.Text.Equals(txtPcidDatabase.Text)
    2. Dim bypassCheckIsNegativeOne As Boolean = BypassCheck_PCID = -1
    3.  
    4. Dim Result = If(systemAndDatabaseAreDifferent And bypassCheckIsNegativeOne, "Yes", "No")

  23. #23

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: [RESOLVED] Conditional Logic In One Line

    Quote Originally Posted by Evil_Giraffe View Post
    Ah! I think I see what your scenario is. You've got something like the following?

    vbnet Code:
    1. If someLongAndConvolutedExpression1 AndAlso someLongAndConvolutedExpression2 Then
    2.     ' Do something
    3. Else
    4.     ' Do something else
    5. End If

    And you're trying to avoid the problem of duplicating the something else:
    vbnet Code:
    1. If someLongAndConvolutedExpression1 Then
    2.     If someLongAndConvolutedExpression2 Then
    3.         ' Do something
    4.     Else
    5.         ' Do something else
    6.     End If
    7. Else
    8.     ' Do something else
    9. End If

    With my revised understanding, I'd ask is this just in the one place that you need this pattern? If so, I probably wouldn't bother with building an abstraction, but consider that you can do this:

    vbnet Code:
    1. Dim condition1 As Boolean = someLongAndConvolutedExpression1
    2. Dim condition2 As Boolean = someLongAndConvolutedExpression2
    3. If condition1 AndAlso condition2 Then
    4.     ' Do something
    5. Else
    6.     ' Do something else
    7. End If

    Saves on all those parentheses and might be more readable:

    vbnet Code:
    1. Dim Result = If((Not txtPcidSystem.Text.Equals(txtPcidDatabase.Text)) And BypassCheck_PCID = -1, "Yes", "No")

    c.f.

    vbnet Code:
    1. Dim systemAndDatabaseAreDifferent As Boolean = Not txtPcidSystem.Text.Equals(txtPcidDatabase.Text)
    2. Dim bypassCheckIsNegativeOne As Boolean = BypassCheck_PCID = -1
    3.  
    4. Dim Result = If(systemAndDatabaseAreDifferent And bypassCheckIsNegativeOne, "Yes", "No")
    Yeah. I'll probably keep it as is. It's only used once.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

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