gpt4 book ai didi

vb.net - 如何重写这个涉及在相等运算符重载中检查三个 Nullable(Of T) 值的条件逻辑?

转载 作者:行者123 更新时间:2023-12-04 06:36:21 26 4
gpt4 key购买 nike

因此,我正在为自定义对象编写相等运算符重载( Operator =() ),而由此产生的 If 条件困惑只是令人眼花缭乱。但到目前为止,这似乎是检查值以匹配该对象的特定行为的唯一明智的方法。

规则是:

  • Num1 是必需的,句点和左右操作数都需要,并且必须等于 True。否则,错误。
  • Num2 是可选的,但如果指定,则左操作数和右操作数都必须存在,并且对于 True 必须相等。否则,错误。
  • Num3 是可选的,但只有在 Num2 也存在时才能指定。否则,错误。
  • Num3(如果指定)必须同时出现在左右操作数中,并且对于 True 必须相等。否则,错误。

  • 这是我迄今为止所拥有的:
    Public Shared Operator =(ByVal lhOp As MyObj, ByVal rhOp As MyObj) As Boolean
    If lhOp Is Nothing Then _
    Return rhOp Is Nothing

    If rhOp Is Nothing Then _
    Return lhOp Is Nothing

    Dim tmpBool As Boolean = ((lhOp.Num1.HasValue AndAlso rhOp.Num1.HasValue) AndAlso (lhOp.Num1.Value = rhOp.Num1.Value))

    '// If tmpbool is 'True', then see if the Num2 HasValue params are equal.
    If tmpBool Then
    If (lhOp.Num2.HasValue = rhOp.Num2.HasValue) Then
    '// They match. Now, do they have any data?
    If (lhOp.Num2.HasValue AndAlso rhOp.Num2.HasValue) Then
    '// Num2 has a value. See if the left and right operands
    '// match each other.
    If (lhOp.Num2.Value = rhOp.Num2.Value) Then
    '// Num2 matches, so see if Num3 HasValue params
    '// are equal.
    If (lhOp.Num3.HasValue = rhOp.Num3.HasValue) Then
    '// They match. Now, do they have any data?
    If (lhOp.Num3.HasValue AndAlso rhOp.Num3.HasValue) Then
    '// Num3 has a value. See if the left and right operands
    '// match each other.
    If (lhOp.Num3.Value = rhOp.Num3.Value) Then
    '// All three parameters match, return 'True'.
    Return True
    Else
    '// Num3 failed to match, return 'False'.
    Return False
    End If
    Else
    '// No data in Num3, return true.
    Return True
    End If
    Else
    '// Num3.HasValues do not match, return false.
    Return False
    End If
    Else
    '// Num2 failed to match, return 'False'.
    Return False
    End If
    Else
    '// No Num2, so we cannot have a Num3 specified as well.
    If (lhOp.Num3.HasValue OrElse rhOp.Num3.HasValue) Then _
    Return False

    '// Return true.
    Return True
    End If
    Else
    '// Num2.HasValues do not match, return false.
    Return False
    End If
    End If

    '// Default is false.
    Return False
    End Operator

    我可以使用三元条件将其折叠到下面的代码块中,这看起来更优雅,但更模糊,不利于适当的维护:
        Return If(((lhOp.Num1.HasValue AndAlso rhOp.Num1.HasValue) AndAlso
    (lhOp.Num1.Value = rhOp.Num1.Value)),
    If((lhOp.Num2.HasValue = rhOp.Num2.HasValue),
    If((lhOp.Num2.HasValue AndAlso rhOp.Num2.HasValue),
    If((lhOp.Num2.Value = rhOp.Num2.Value),
    If((lhOp.Num3.HasValue = rhOp.Num3.HasValue),
    If((lhOp.Num3.HasValue AndAlso rhOp.Num3.HasValue),
    If((lhOp.Num3.Value = rhOp.Num3.Value),
    True, False),
    True),
    False),
    False),
    If((lhOp.Num3.HasValue OrElse rhOp.Num3.HasValue), False, True)),
    False),
    False)

    我也知道我可以将检查的元素缓存到 bool 值中,然后使用它们来减少文本量。但这似乎只是为了代码可读性而浪费周期。所以我想知道 SO 社区是否对重组它有更好的想法,以防我的检查过于冗长。

    最佳答案

    我想你会发现这里大多数已经编程了一段时间的人会建议尽快从方法返回。否则你最终会永远缩进你的代码并试图用 End XXX 或大括号来匹配逻辑。

    所以这个简化的类应该与你的大致匹配:

    Public Class MyObj
    Public Property Num1 As Boolean?
    Public Property Num2 As Boolean?
    Public Property Num3 As Boolean?
    Public Sub New(ByVal n1 As Boolean?, ByVal n2 As Boolean?, ByVal n3 As Boolean?)
    Me.Num1 = n1
    Me.Num2 = n2
    Me.Num3 = n3
    End Sub
    End Class

    根据您的逻辑,此方法应用作比较器:
    Public Shared Function CheckEquals(ByVal lhOp As MyObj, ByVal rhOp As MyObj) As Boolean
    '//Num1 is required, also check the others for mismatched properties
    If (Not lhOp.Num1.HasValue) OrElse (Not rhOp.Num1.HasValue) OrElse (lhOp.Num2.HasValue <> rhOp.Num2.HasValue) OrElse (lhOp.Num3.HasValue <> rhOp.Num3.HasValue) Then Return False

    '//Num1 is required and must have the same value
    If (lhOp.Num1.Value <> rhOp.Num1.Value) Then Return False

    '//If Num2 is present the values must match
    If (lhOp.Num2.HasValue) AndAlso (lhOp.Num2.Value <> rhOp.Num2.Value) Then Return False
    '//Num3 is optional but Num2 must be set
    If lhOp.Num3.HasValue AndAlso (Not lhOp.Num2.HasValue OrElse (lhOp.Num3.Value <> lhOp.Num3.Value)) Then Return False

    Return True
    End Function

    这里有一堆测试可以证明这一点:
        Debug.Assert(CheckEquals(New MyObj(True, Nothing, Nothing), New MyObj(True, Nothing, Nothing)) = True)

    Debug.Assert(CheckEquals(New MyObj(True, Nothing, Nothing), New MyObj(True, True, Nothing)) = False)
    Debug.Assert(CheckEquals(New MyObj(True, Nothing, Nothing), New MyObj(True, Nothing, True)) = False)
    Debug.Assert(CheckEquals(New MyObj(True, Nothing, Nothing), New MyObj(True, True, True)) = False)

    Debug.Assert(CheckEquals(New MyObj(True, Nothing, Nothing), New MyObj(True, False, Nothing)) = False)
    Debug.Assert(CheckEquals(New MyObj(True, Nothing, Nothing), New MyObj(True, Nothing, False)) = False)
    Debug.Assert(CheckEquals(New MyObj(True, Nothing, Nothing), New MyObj(True, False, False)) = False)

    Debug.Assert(CheckEquals(New MyObj(True, True, Nothing), New MyObj(True, True, Nothing)) = True)
    Debug.Assert(CheckEquals(New MyObj(True, True, Nothing), New MyObj(True, True, True)) = False)
    Debug.Assert(CheckEquals(New MyObj(True, True, Nothing), New MyObj(True, True, False)) = False)

    Debug.Assert(CheckEquals(New MyObj(True, True, Nothing), New MyObj(True, Nothing, True)) = False)
    Debug.Assert(CheckEquals(New MyObj(True, True, Nothing), New MyObj(True, Nothing, False)) = False)

    Debug.Assert(CheckEquals(New MyObj(True, Nothing, True), New MyObj(True, Nothing, True)) = False)
    Debug.Assert(CheckEquals(New MyObj(True, Nothing, True), New MyObj(True, Nothing, False)) = False)
    Debug.Assert(CheckEquals(New MyObj(True, True, True), New MyObj(True, True, True)) = True)
    Debug.Assert(CheckEquals(New MyObj(True, True, False), New MyObj(True, True, False)) = True)
    Debug.Assert(CheckEquals(New MyObj(True, False, False), New MyObj(True, False, False)) = True)
    Debug.Assert(CheckEquals(New MyObj(True, False, True), New MyObj(True, False, True)) = True)

    关于vb.net - 如何重写这个涉及在相等运算符重载中检查三个 Nullable(Of T) 值的条件逻辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4825529/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com