-
Feb 27th, 2012, 01:50 PM
#1
[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:
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:
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
Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Feb 27th, 2012, 02:02 PM
#2
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
-
Feb 27th, 2012, 02:09 PM
#3
Addicted Member
Re: Conditional Logic In One Line
i would do it like this:
vb Code:
If Not (Me.txtPcidSystem.Text = Me.txtPcidDatabase.Text And TestData.BypassCheck_PCID = -1) Then
-
Feb 27th, 2012, 02:20 PM
#4
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.
-
Feb 27th, 2012, 02:28 PM
#5
Re: Conditional Logic In One Line
Originally Posted by kevininstructor
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
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.
Originally Posted by vb_ftw
i would do it like this:
vb Code:
If Not (Me.txtPcidSystem.Text = Me.txtPcidDatabase.Text And TestData.BypassCheck_PCID = -1) Then
Works perfectly. I had my parentheses mixed up.
Originally Posted by DataMiser
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
Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Feb 27th, 2012, 02:29 PM
#6
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")
-
Feb 27th, 2012, 02:36 PM
#7
Re: Conditional Logic In One Line
Originally Posted by kevininstructor
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
Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Feb 27th, 2012, 05:44 PM
#8
Re: [RESOLVED] Conditional Logic In One Line
Originally Posted by weirddemon
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?
-
Feb 27th, 2012, 05:57 PM
#9
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
-
Feb 27th, 2012, 07:47 PM
#10
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.
-
Feb 27th, 2012, 07:53 PM
#11
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.
-
Feb 28th, 2012, 12:53 PM
#12
Re: [RESOLVED] Conditional Logic In One Line
Originally Posted by Evil_Giraffe
I don't understand this. Could you explain please?
Originally Posted by DataMiser
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
Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Feb 28th, 2012, 04:33 PM
#13
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:
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
-
Feb 28th, 2012, 04:35 PM
#14
Re: [RESOLVED] Conditional Logic In One Line
Originally Posted by kaliman79912
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:
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
Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Feb 28th, 2012, 05:03 PM
#15
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.
-
Feb 28th, 2012, 05:05 PM
#16
Re: [RESOLVED] Conditional Logic In One Line
Originally Posted by DataMiser
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
Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Feb 28th, 2012, 05:46 PM
#17
Re: [RESOLVED] Conditional Logic In One Line
Originally Posted by weirddemon
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
-
Feb 28th, 2012, 05:55 PM
#18
Re: [RESOLVED] Conditional Logic In One Line
Originally Posted by weirddemon
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?]
-
Feb 28th, 2012, 06:07 PM
#19
Re: [RESOLVED] Conditional Logic In One Line
Originally Posted by Evil_Giraffe
[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
-
Feb 28th, 2012, 06:29 PM
#20
Re: [RESOLVED] Conditional Logic In One Line
Originally Posted by kaliman79912
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
Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Feb 28th, 2012, 06:33 PM
#21
Re: [RESOLVED] Conditional Logic In One Line
Originally Posted by Evil_Giraffe
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
Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Feb 28th, 2012, 07:42 PM
#22
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:
If someLongAndConvolutedExpression1 AndAlso someLongAndConvolutedExpression2 Then
' Do something
Else
' Do something else
End If
And you're trying to avoid the problem of duplicating the something else:
vbnet Code:
If someLongAndConvolutedExpression1 Then
If someLongAndConvolutedExpression2 Then
' Do something
Else
' Do something else
End If
Else
' Do something else
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:
Dim condition1 As Boolean = someLongAndConvolutedExpression1
Dim condition2 As Boolean = someLongAndConvolutedExpression2
If condition1 AndAlso condition2 Then
' Do something
Else
' Do something else
End If
Saves on all those parentheses and might be more readable:
vbnet Code:
Dim Result = If((Not txtPcidSystem.Text.Equals(txtPcidDatabase.Text)) And BypassCheck_PCID = -1, "Yes", "No")
c.f.
vbnet Code:
Dim systemAndDatabaseAreDifferent As Boolean = Not txtPcidSystem.Text.Equals(txtPcidDatabase.Text)
Dim bypassCheckIsNegativeOne As Boolean = BypassCheck_PCID = -1
Dim Result = If(systemAndDatabaseAreDifferent And bypassCheckIsNegativeOne, "Yes", "No")
-
Feb 29th, 2012, 12:29 AM
#23
Re: [RESOLVED] Conditional Logic In One Line
Originally Posted by Evil_Giraffe
Ah! I think I see what your scenario is. You've got something like the following?
vbnet Code:
If someLongAndConvolutedExpression1 AndAlso someLongAndConvolutedExpression2 Then
' Do something
Else
' Do something else
End If
And you're trying to avoid the problem of duplicating the something else:
vbnet Code:
If someLongAndConvolutedExpression1 Then
If someLongAndConvolutedExpression2 Then
' Do something
Else
' Do something else
End If
Else
' Do something else
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:
Dim condition1 As Boolean = someLongAndConvolutedExpression1
Dim condition2 As Boolean = someLongAndConvolutedExpression2
If condition1 AndAlso condition2 Then
' Do something
Else
' Do something else
End If
Saves on all those parentheses and might be more readable:
vbnet Code:
Dim Result = If((Not txtPcidSystem.Text.Equals(txtPcidDatabase.Text)) And BypassCheck_PCID = -1, "Yes", "No")
c.f.
vbnet Code:
Dim systemAndDatabaseAreDifferent As Boolean = Not txtPcidSystem.Text.Equals(txtPcidDatabase.Text)
Dim bypassCheckIsNegativeOne As Boolean = BypassCheck_PCID = -1
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|