vba 将数组放入 class.property

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9720018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:45:56  来源:igfitidea点击:

get an array into a class.property

vba

提问by user1266138

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

我得到错误 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)

现在将它们标记为 public 并添加了初始化和 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

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

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

回答by assylias

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

您还需要初始化 pBonds 数组,否则第一次调用 UBound 时会出现错误:

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

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

回答by JacobHung

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

回答by Alex K.

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

Private如果您想将它们公开给自动化客户端,您的类方法会被标记Public

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

(您还需要括号返回数组:Public Property Get Bonds() As String()