Is there a way to write an equality test for a VBA class with private members without exposing knowledge of the existence of those private members?

前端 未结 3 2238

I do a fair amount of Excel VBA programming, but not a lot of it is object-oriented. Here is something that comes up every now and then that bugs me, and I\'m wondering if t

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-31 23:18

    I would write the class like this

    Private mdhidden1_ As Double
    Private mdhidden2_ As Double
    
    Public Property Get hidden1_() As Double
    
        hidden1_ = mdhidden1_
    
    End Property
    
    Public Property Get hidden2_() As Double
    
        hidden2_ = mdhidden2_
    
    End Property
    
    Private Sub Class_Initialize()
    
        'some method of setting variables private to the class
        mdhidden1_ = 1
        mdhidden2_ = 2
    
    End Sub
    
    Public Property Get IsEquivalent(clsCompare As C) As Boolean
    
        IsEquivalent = Me.hidden1_ = clsCompare.hidden1_ And Me.hidden2_ = clsCompare.hidden2_
    
    End Property
    

    If you're forced to expose knowledge of the member anyway, you may as well make it a read-only property (Get, but no Let). Then you can make the IsEquivalent boolean property inside the class.

提交回复
热议问题