Get array in class.property

I have a class with the following properties:

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

      

when i try:

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

      

I get an error Method or data member not found

Any idea where this came from?


made changes

marked them as public and added initialization and byval (got another error without it)

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

      

error: The property procedure definitions for the same property are inconsistent or the property procedure has an optional parameter, a ParamArray parameter or an invalid final parameter can help me with this? thank

+3


source to share


3 answers


You also need to initialize the pBonds array, or you will get an error the first time you call UBound:

The main module

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 class

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

      

+2


source


Your class methods are flagged Private

, if you want to expose them to automation clients, make them Public

.



(You also need to return an array of parsers: Public Property Get Bonds() As String()

)

+1


source


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

      

+1


source







All Articles