我有一個具有以下屬性的類:得到一個數組到class.property
Dim pBonds() as string
Private Property Get Bonds() As String
Bonds = pBonds
End Property
Private Property Get Bond(index As Long) As String
Bond = pBonds(index)
End Property
Private Property Let Bond(index As Long, strValue As String)
If index > UBound(pBonds) Then ReDim Preserve pBonds(index)
pBond(index) = strValue
End Property
當我嘗試:
Set o = New CBondBasket
For k = LBound(arr) To UBound(arr)
o.Bond(k) = arr(k)
Next k
我得到錯誤Method or data member not found
任何想法,那來自?
所做的更改
將其標示爲公共現在並添加初始化和BYVAL
Private Sub Class_Initialize()
ReDim pBonds(0)
End Sub
Public Property Get Bonds() As String()
Bonds = pBonds
End Property
Public Property Get Bond(index As Long) As String
Bond = pBonds(index)
End Property
Public Property Let Bond(ByVal index As Long, ByVal strValue As String)
If index > UBound(pBonds) Then ReDim Preserve pBonds(index)
pBonds(index) = strValue
End Property
錯誤的是(W/O它讓我另一個錯誤):爲屬性過程定義相同的屬性不一致或屬性過程有一個可選的參數,一個ParamArray或一個無效的最終參數設置可以幫助我嗎?謝謝
這整個方法是註定的。只要將數組公開給公共屬性就可以廢棄所有這些多餘的存取方法,或者根本不公開*。 – Tomalak 2012-03-15 13:07:09
Tomalak,不確定你的意思。你可以給我一個例子嗎?那裏我很新。 – user1266138 2012-03-15 14:56:32
@ user1266138我認爲他的意思是使用getters和setter的目的是隱藏你的底層實現。如果你使用getter和setter來返回所有的私有字段,那麼目的就會被打敗,並且公開這些字段會更容易。 – assylias 2012-03-15 16:30:19