3

我在 VBA (Excel 2010) 中的自定义类中遇到Get属性问题。如果没有给出索引参数,那么我的 Get 属性应该返回对 Class 数组的引用(至少这是我的印象)。如果给定索引,它应该返回私有数组中给定索引中的值。

' Custom Class Properties
Private pMtbSheets() As String

'Get and Let Methods
Public Property Get MtbSheets(Optional index As Variant) As String()
    If IsMissing(index) Then
        ReDim MtbSheets(1 To UBound(pMtbSheets))
        MtbSheets = pMtbSheets()
    Else
        ReDim MtbSheets(1 To 1)
        MtbSheets(1) = pMtbSheets(index) '**Compiler error occures here**
    End If
End Property

感谢任何人能够提供的任何帮助

4

2 回答 2

2

您需要一个临时数组来避免MtbSheets(i)被解释为属性/方法/函数调用与数组访问之间的歧义:

ReDim temp(1 To 1) As String
temp(1) = pMtbSheets(index)
MtbSheets = temp 
于 2016-04-06T13:35:10.793 回答
0

编辑:我的回答当然行不通,您需要使用 Alex K. 在他的回答中提到的临时数组。

只需像在 IsMissing() 分支中一样返回数组:

' Custom Class Properties
Private pMtbSheets() As String

'Get and Let Methods
Public Property Get MtbSheets(Optional index As Variant) As String()
    If IsMissing(index) Then
        ReDim MtbSheets(1 To UBound(pMtbSheets))
        MtbSheets = pMtbSheets()
    Else
        ReDim MtbSheets(1 To 1)
        MtbSheets = pMtbSheets(index)
    End If
End Property
于 2016-04-06T13:37:06.437 回答