1

我有一个看起来像这样的类:

Public Class LumberPiece
    Public boardLength As Double
    Public currentLeftEndPosition As Double
    Public currentRightEndPosition As Double

    Public Sub New(ByVal bl As Double, ByVal clp As Double)
        boardLength = clsDimension.ConvertInchesToMillimeters(bl)
        currentLeftEndPosition = clsDimension.ConvertInchesToMillimeters(clp)
        currentRightEndPosition = clsDimension.ConvertInchesToMillimeters(clp + bl)
    End Sub
End Class

我想以某种方式有一个触发器,每次更改 currentLeftEndPosition 时计算 currentRightEndPosition(rightEnd 始终等于 leftend+ boardLength)。

我怎样才能做到这一点?

4

1 回答 1

2

您可以使用属性来执行此操作。通过设置字段属性,您可以控制读取和设置属性值时发生的情况。例如:

Public Class LumberPiece
    Public Property BoardLength As Double

    Public Property CurrentLeftEndPosition As Double
        Get
            Return _currentLeftEndPosition
        End Get
        Set(value As Double)
            _currentLeftEndPosition = value
            CurrentRightEndPosition = clsDimension.ConvertInchesToMillimeters(_currentLeftEndPosition + BoardLength)
        End Set
    End Property
    Private _currentLeftEndPosition As Double

    Public Property CurrentRightEndPosition As Double

    Public Sub New(ByVal bl As Double, ByVal clp As Double)
        BoardLength = clsDimension.ConvertInchesToMillimeters(bl)
        CurrentLeftEndPosition = clsDimension.ConvertInchesToMillimeters(clp)
    End Sub
End Class

或者,您可以选择在每次访问时计算该字段:

Public Class LumberPiece
    Public Property BoardLength As Double
    Public Property CurrentLeftEndPosition As Double

    Public ReadOnly Property CurrentRightEndPosition As Double
        Get
            Return clsDimension.ConvertInchesToMillimeters(CurrentLeftEndPosition + BoardLength)
        End Get
    End Property

    Public Sub New(ByVal bl As Double, ByVal clp As Double)
        BoardLength = clsDimension.ConvertInchesToMillimeters(bl)
        CurrentLeftEndPosition = clsDimension.ConvertInchesToMillimeters(clp)
    End Sub
End Class
于 2013-07-30T15:55:16.767 回答