2012-03-15 39 views
3

我有一個具有以下屬性的類:得到一個數組到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或一個無效的最終參數設置可以幫助我嗎?謝謝

+2

這整個方法是註定的。只要將數組公開給公共屬性就可以廢棄所有這些多餘的存取方法,或者根本不公開*。 – Tomalak 2012-03-15 13:07:09

+0

Tomalak,不確定你的意思。你可以給我一個例子嗎?那裏我很新。 – user1266138 2012-03-15 14:56:32

+0

@ user1266138我認爲他的意思是使用getters和setter的目的是隱藏你的底層實現。如果你使用getter和setter來返回所有的私有字段,那麼目的就會被打敗,並且公開這些字段會更容易。 – assylias 2012-03-15 16:30:19

回答

1

如果您想將它們公開給自動化客戶端使其成爲Public,那麼您的類方法標記爲Private

(你還需要括號返回數組:Public Property Get Bonds() As String()

2

您還需要初始化pBonds陣列或者你會得到一個錯誤調用UBound函數第一次時:

主要模塊

Option Explicit 

Sub testClass() 

    Dim o As CBondBasket 
    Dim k As Long 
    Dim arr As Variant 

    arr = Array(1, 2, 3, 4, 5) 

    Set o = New CBondBasket 
    For k = LBound(arr) To UBound(arr) 
     o.Bond(k) = arr(k) 
    Next k 

    For k = LBound(o.Bonds) To UBound(o.Bonds) 
     Debug.Print o.Bond(k) 
    Next k 

End Sub 

類CBondBasket

Private pBonds() As String 

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(index As Long, strValue As String) 
    If index > UBound(pBonds) Then ReDim Preserve pBonds(index) 
    pBonds(index) = strValue 
End Property 
+0

將其標示爲公共現在並添加初始化和BYVAL(讓我另一個錯誤W/O它) 私人小組Class_Initialize() 使用ReDim pBonds(0) 結束小組 公共屬性獲取債券()作爲字符串() 債券= pBonds 高端物業 公共財產獲取債券(指數只要)作爲字符串 債券= pBonds(指數) 高端物業 公共財產令債券(BYVAL指數長,BYVAL strValue中作爲字符串) 如果指數> UBound(pBonds)然後ReDim保存pBonds(索引號) 個pBonds(指數)= strValue中 高端物業 – user1266138 2012-03-15 13:14:09

+0

誤差的屬性過程 定義爲相同的屬性不一致或財產過程有一個可選參數,一個ParamArray或無效的組最終參數 任何幫助嗎? – user1266138 2012-03-15 13:21:21

+0

@ user1266138這很難在評論中閱讀 - 你可以用你的新代碼編輯你的問題嗎? – assylias 2012-03-15 13:28:43

1
Option Compare Database 

Option Explicit 

Public Function test1() As Integer 

    Dim sdate(2) As Date 
    Dim edate(2) As Date 
    Dim serdat As Class_erviceDate 

    sdate(1) = #1/2/2015# 
    edate(1) = #10/21/2015# 
    sdate(2) = #2/5/2015# 
    edate(2) = #12/25/2015# 

    Set serdat = New Class_ServiceDate 
    serdat.serviceStart = sdate 
    serdat.serviceEnd = edate 

    Debug.Print serdat.serviceStart(1), serdat.serviceEnd(1) 
    Debug.Print serdat.serviceStart(2), serdat.serviceEnd(2) 

End Function 


Option Compare Database 

Option Explicit 

Private f_datServiceStart As Variant 
Private f_datServiceEnd As Variant 

Public Property Get serviceStart() As Variant 

    serviceStart = f_datServiceStart 

End Property 

Public Property Let serviceStart(Value As Variant) 

    f_datServiceStart = Value 

End Property 

Public Property Get serviceEnd() As Variant 

    serviceEnd = f_datServiceEnd 

End Property 

Public Property Let serviceEnd(Value As Variant) 

    f_datServiceEnd = Value 

End Property