Search code examples
vb.netxor

Xor doesn't fire


No more answers please, the problem was solved. Skip to the end of the question to see what i have done wrong.


I am running the following Functionto read the syntax of a specific standard either by FilePath (Function reads the file first to get the string) or by the Text itself (skips file reading)

Public Function ReadStandard(Optional ByVal FilePath As String = Nothing, _
Optional ByVal Standard_Text As String = Nothing) As Boolean

to make this possible only one of those parameter must be set, while the other must not be set. I do not want to use a function like

Public Function ReadStandard(str as String, isFilePath as Booelean) As Boolean

So to make this possible I want to use Xor, since it should do the exact job (if you pass 2 Booleans to XOR it should only return True, when A != B). Doing some research i found that this works in vb.net: MSDN

But for some reason it doesn't work for me;

If IsNothing(FilePath) Xor IsNothing(Standard_Text) Then (...)

Is there a reason for it? Or did I forget what i learned back in the days?


Turns out that I just mixed something up in my logic. In the following function I forgot to put the Not into the If-Statement

If Not (IsNothing(FilePath) Xor IsNothing(Standard_Text)) Then
  Throw New ArgumentException("FilePath or Standard_Text must be set.")
  Return False
End If

Solution

  • XOR can be thought of as binary addition without carry.

        If False Xor False Then '0+0
            Stop
        End If
    
        If True Xor False Then '1+0
            Stop
        End If
    
        If False Xor True Then '0+1
            Stop
        End If
    
        If True Xor True Then '1+1
            Stop
        End If