我在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
感谢任何人能够提供的任何帮助
答案 0 :(得分:2)
您需要一个临时数组,以避免MtbSheets(i)
被解释为属性/方法/函数调用与数组访问之间的歧义:
ReDim temp(1 To 1) As String
temp(1) = pMtbSheets(index)
MtbSheets = temp
答案 1 :(得分: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