Reputation: 3
Am I just being blind or does this if
statement genuinely not do what it's meant to?
Dim textSample as String = "F"
If Not textSample = "D" Or Not textSample = "E" Or Not textSample = "F" Then
MessageBox.Show("False")
End If
This displays the message box even though textSample is one of the letters. In my eyes that if
statement should see that textSample is one of those letters and skip it, whereas if it was Z it would "Not" be equal to any of those and would therefore show the message box.
Why does it step into the if
statement?
Upvotes: 0
Views: 836
Reputation: 2136
The message will always show. Here is why. In your example let us say textSample = "F". Then
if Not F equals D Or Not F equals E or Not F equals F
So let us summarize:
if (F not equals D ) or ( F not equals E ) or ( F not equals F)
... if ( true ) or (true) or (false)
So your condition is true no matter what textSample is... (except if your textSample could be at the same be equals to "D" and equals to "E" and equals to "F").
I think you want to change the "or" to "and".
Upvotes: 0
Reputation: 557
There is no value of textSample for which your if condition could possibly be false. I think you want this instead:
If Not (textSample = "D" Or textSample = "E" Or textSample = "F") Then
MessageBox.Show("False")
If you don't see the difference, examine the truth tables for both versions.
Upvotes: 2
Reputation: 20415
I would personally write it like so:
Dim textSample As String = "F"
If textSample <> "D" AndAlso textSample <> "E" AndAlso textSample <> "F" Then
MessageBox.Show("False")
End If
If you, like me, like to use the chainability of .NET, I also wrote for myself several String Extensions for cases like this:
Public Module StringExtensions
<Extension()> _
Public Function IsNullOrBlank(ByVal s As String) As Boolean
Return s Is Nothing OrElse s.Trim.Length.Equals(0)
End Function
<Extension()> _
Public Function IsNotNullOrBlank(ByVal s As String) As Boolean
Return s IsNot Nothing AndAlso s.Trim.Length > 0
End Function
<Extension()> _
Public Function IsEqualToAny(ByVal s As String, ByVal ParamArray values As String()) As Boolean
If s.IsNotNullOrBlank AndAlso values.Length > 0 Then
For Each value As String In values
If s = value Then Return True
Next
End If
Return False
End Function
<Extension()> _
Public Function IsNotEqualToAny(ByVal s As String, ByVal ParamArray values As String()) As Boolean
If s.IsNotNullOrBlank AndAlso values.Length > 0 Then
For Each value As String In values
If s = value Then Return False
Next
End If
Return True
End Function
End Module
To where I could then write your If statement like so:
Dim textSample As String = "F"
If textSample.IsNotEqualToAny("D", "E", "F") Then
MessageBox.Show("False")
End If
Upvotes: 0
Reputation: 33738
It is acting normally. True Or True Or False = True
I believe what you want is
Dim tBadLetters() As String = {"D", "E", "F"}
If Not tBadLetters.COntains(txtSample)
MsgBox("blah")
End If
Upvotes: 3
Reputation: 3074
It's because your using an OR clause, you need to use AND. Basically your saying if the textSample is not D then show your message box.
Change it to:
Dim textSample as String = "F"
If Not textSample = "D" AND Not textSample = "E" AND Not textSample = "F" Then
MessageBox.Show("False")
End If
That should work.
Upvotes: 2
Reputation: 370122
cond1 Or cond2 Or ... Or condn
is true if and only if at least one of the given conditions are true. In your case it is always the case that at least one of the conditions is true (in fact at least two of the conditions will be true in each case). For example if textSample
is "D"
then the condition Not textSample = "E"
and the condition Not textSample = "F"
will be true. So the whole condition will be true.
Long story short: Use And instead of Or.
Upvotes: 5