Xor 不触发

Xor doesn't fire

没有更多的答案,问题已解决。跳到问题的最后看看我做错了什么。


我是运行以下Function通过FilePath阅读特定标准的语法(Function阅读file first得到字符串) 或 Text 本身 (skips file reading)

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

要做到这一点,只需设置其中一个参数,而不能设置另一个。我不想使用像

这样的函数
Public Function ReadStandard(str as String, isFilePath as Booelean) As Boolean

因此,为了使这成为可能,我想使用 Xor,因为它应该能完成准确的工作 (如果你将 2 个布尔值传递给 XOR,它应该只 return True,当 A != B 时)。做一些研究我发现这在 vb.net 中有效:MSDN

但出于某种原因,它对我不起作用;

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

有什么原因吗?还是我忘记了那些日子里学到的东西?


原来我只是在我的逻辑中混淆了一些东西。在下面的函数中,我忘记将 Not 放入 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

你的来电怎么样?我试过你的样本,它正在工作。

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

这样调用(X 是示例路径)提供正确答案:

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

注意像 ReadStandard("", "X") 这样的调用,因为这意味着 FilePath 不为空。

BR

异或可以认为是无进位的二进制加法。

    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