gpt4 book ai didi

arrays - VBA 编译错误 : Can't Assign to Read-Only Property Returning Array from Custom Class Get Property

转载 作者:行者123 更新时间:2023-12-01 15:49:42 25 4
gpt4 key购买 nike

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

' 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

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

最佳答案

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

ReDim temp(1 To 1) As String
temp(1) = pMtbSheets(index)
MtbSheets = temp

关于arrays - VBA 编译错误 : Can't Assign to Read-Only Property Returning Array from Custom Class Get Property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36452556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com