arrays - 为什么 VBA 类不能通过 ByRef Array()?

标签 arrays excel vba class byref

我正在使用 EXCEL 2007
我构建了这段代码,它从 EXCEL 单元格中获取数据,并将名为 MesPoints() 的 Array() 填充为 clsPoint。该代码还使用一个名为 MaCurve 的数组作为 clsCurve。该类在内部定义了一个名为 PointsXY 的变体数组。我想避免将所有数据复制到类中,所以我试图传递整个数组 ByRef。在一个模块中,我将数据填充到 MesPoints 中,然后尝试将 MesPoints 作为 Variant 传递到类中。默认情况下,这应该由 ByRef 完成。
下面的代码显示了如何调用 Property Let AllDots 以将整个 Array 作为 Variant clsPoint 类传递。

Private MaCurve As New clsCurve 
Private MesPoints() As New clsPoint
Dim MyRange As Range
Dim MySheet As Worksheet
Dim StartRow As Long, EndRow As Long, StartCol As Long

Sub FillClasses() ' This code in into a separate module Main
Dim Pcount As Long, Scount As Long, Ccount As Long, Dcount As Long
Dim i As Integer, j As Integer, k As Integer, m As Long, pr As Long, pc As Long
Dim Y As Double, X As Double

StartRow = 4: EndRow = 9: StartCol = 2
Set MySheet = Application.Worksheets("Data1")
Set MyRange = Range(MySheet.Cells(StartRow, StartCol), MySheet.Cells(EndRow, StartCol + 1)) ' Défini le range des données
Pcount = MyRange.Rows.Count: Scount = Pcount - 1: Ccount = Scount - 1: Dcount = Ccount - 1

ReDim MesPoints(Pcount)
    For i = 1 To Pcount ' Population des valeurs X et Y des points
        j = 1: k = 2
        MesPoints(i).X = MyRange.Cells(i, j)
        MesPoints(i).Y = MyRange.Cells(i, k)
    Next i

Let MaCurve.AllDots = MesPoints()
'......_____________________________________________________________________________________________
'  Class MaCurve
Dim PointsXY As Variant
Public Property Let AllDots(ByRef AP() As clsPoint)
Let PointsXY = AP()
DataCount = UBound(PointsXY())
Pcount = DataCount + 1
ReDim Preserve PointsXY(Pcount)
Scount = DataCount: Ccount = Scount - 1: Dcount = Ccount - 1
    For i = 1 To Scount ' Population des droites
        Set Segments(i).P1 = PointsXY(i) ' The code will choke here
    Next i


'...

End Property
该代码将不起作用。相反,只要调用 PointsXY 中的数据之一进行计算
处理器退出类代码,没有错误,没有警告,没有任何东西,在主代码中返回焦点
并继续。
我是在正确调用 Property Let 吗?
错误在哪里,因为这是一种奇怪的 VBA 行为?

最佳答案

Property Let的value参数(或 Property Set ,就此而言)是 always passed by value ,无论说明符如何。这根本不是数组独有的,但您可以通过传递 Variant 来解决它。指针而不是显式的类型化数组:

Public Property Let AllDots(ByVal Values As Variant)
我丢失了链接,但这是相关的语言规范部分:

§ If the <value-param> of a <property-LHS-declaration> does not have a <parameter-mechanism> element or has a <parameter-mechanism> consisting of the keyword ByRef, it has the same meaning as if it instead had a <parameter-mechanism> element consisting of the keyword ByVal.


查看讨论 herehere (GitHub 链接到 Rubberduck 存储库)。

关于arrays - 为什么 VBA 类不能通过 ByRef Array()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71301764/

相关文章:

php - 如何使用 codeigniter 加载自定义配置文件?

vba - 使用 VBA 将两个不同工作表上的多个范围保存为 PDF

vba - 禁用形状的右键菜单

excel - 如何调试 COM C++ 进程外服务器? (客户端: Excel vba)

javascript - 多维数组的嵌套 ng-repeat

c - 以下冒泡排序在 C 中不起作用

java - Java中如何优雅的复制多维数组?

python - 为什么只有一行导出到excel?

excel - 如何解决 Power BI 错误 - key 与表中的任何行都不匹配

excel - `WEEKDAY` 函数给出了错误的值?