user1266138
user1266138

Reputation: 625

get an array into a 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 error Method or data member not found

Any idea where that comes from?


made the changes

marked them as public now and added initialization and byval (got me another error w/o 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 is: Definitions of property procedures for the same property are inconsistent or property procedure has an optional parameter, a ParamArray or an invalid set final parameter can anyone help me with that? thanks

Upvotes: 4

Views: 25250

Answers (3)

JacobHung
JacobHung

Reputation: 11

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

Upvotes: 1

assylias
assylias

Reputation: 328598

You also need to initialise the pBonds array or you will get an error when calling UBound the first time:

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

Class 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

Upvotes: 5

Alex K.
Alex K.

Reputation: 175766

Your class methods are marked Private if you want to expose them to automation clients make them Public.

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

Upvotes: 1

Related Questions