将数组放入 class.property

use*_*138 4 vba

我有一个具有以下属性的类:

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
Run Code Online (Sandbox Code Playgroud)

当我尝试时:

Set o = New CBondBasket
   For k = LBound(arr) To UBound(arr)
       o.Bond(k) = arr(k)
   Next k
Run Code Online (Sandbox Code Playgroud)

我收到错误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
Run Code Online (Sandbox Code Playgroud)

错误是:同一属性的属性过程的定义不一致,或者属性过程有可选参数、ParamArray 或无效的设置最终参数,有人可以帮助我吗?谢谢

ass*_*ias 5

您还需要初始化 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
Run Code Online (Sandbox Code Playgroud)

CB 类债券一篮子

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
Run Code Online (Sandbox Code Playgroud)