получить массив в class.property - PullRequest
3 голосов
/ 15 марта 2012

У меня есть класс со следующими свойствами:

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

ошибка: Определения процедур свойств для одного и того же свойства противоречивы, или процедура свойств имеет необязательный параметр, ParamArray или недопустимый заданный конечный параметр, может кто-нибудь мне помочь с этим? спасибо

Ответы [ 3 ]

2 голосов
/ 15 марта 2012

Вам также нужно инициализировать массив 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
1 голос
/ 11 сентября 2015
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 голос
/ 15 марта 2012

Ваши методы класса помечены Private, если вы хотите предоставить их клиентам автоматизации, сделайте их Public.

(Вам также нужны парены, чтобы вернуть массив: Public Property Get Bonds() As String())

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...