Luke
Luke

Reputation: 781

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

Upvotes: 0

Views: 76

Answers (2)

dbasnett
dbasnett

Reputation: 11773

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

Upvotes: 1

pLurchi
pLurchi

Reputation: 61

how does your call look like? I tried your sample and it is working.

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

    ReadStandard = False
    If IsNothing(FilePath) Xor IsNothing(Standard_Text) Then
        ReadStandard = True
    End If

End Function

Calling like this (X is example path) delivers the right answer:

ReadStandard(,)         --> False
ReadStandard(, "X")     --> True
ReadStandard("X",)      --> True
ReadStandard("X", "X")  --> False

Be aware of calling like ReadStandard("", "X"), because that means FilePath is not empty.

BR

Upvotes: 0

Related Questions