gpt4 book ai didi

arrays - 将数组分配给属性 let 时行为不一致

转载 作者:行者123 更新时间:2023-12-02 01:24:19 25 4
gpt4 key购买 nike

考虑以下代码:

Option Explicit

Public Property Let Dummy(v() As Integer)

End Property

Public Sub LetDummy(v() As Integer)

End Sub

Sub Foo()
ReDim i(0 To 2) As Integer
i(0) = 3
i(1) = 45
i(2) = 10

Bar i ' internal error 51
End Sub

Sub Foo2()
ReDim i(0 To 2) As Integer
i(0) = 3
i(1) = 45
i(2) = 10

Bar2 i ' no error
End Sub

Sub Foo3()
ReDim i(0 To 2) As Integer
i(0) = 3
i(1) = 45
i(2) = 10

Dummy = i ' no error
End Sub

Sub Bar(j() As Integer)
Dummy = j
End Sub

Sub Bar2(j() As Integer)
LetDummy j
End Sub

当我运行宏“Foo”时,我收到消息“内部错误 51”,但“Foo2”和“Foo3”运行正常。这种行为的原因是什么?或者这只是 VBA 中的一个错误?如何修复这个错误?

背景:在我的应用程序中,我想将作为函数参数提供的数组分配给数组类型的属性。

最佳答案

对我来说,这看起来像是一个错误,因为错误 51 表示如果收到它,请联系 MS。

如果我创建一个局部变量,分配它,然后传递它,它似乎可以工作。

非常老套。

Option Explicit

Private ary() As Integer

Public Property Get Dummy() As Integer()
Dummy = ary
End Property

Public Property Let Dummy(v() As Integer)
Debug.Print "In Dummy" & UBound(v)
ary = v

End Property

Public Sub LetDummy(v() As Integer)
Debug.Print "In LetDummy" & UBound(v)
End Sub

Sub Foo()
ReDim i(0 To 2) As Integer
i(0) = 3
i(1) = 45
i(2) = 10

Call Bar(i) ' internal error 51
End Sub

Sub Foo2()
ReDim i(0 To 2) As Integer
i(0) = 3
i(1) = 45
i(2) = 10

Bar2 i ' no error
End Sub

Sub Foo3()
ReDim i(0 To 2) As Integer
i(0) = 3
i(1) = 45
i(2) = 10

Dummy = i ' no error
End Sub

Sub Bar(j() As Integer)
Dim i() As Integer
i = j

Dummy = i

Dim x As Integer
Dim myary() As Integer
myary = Dummy
For x = 0 To 2
Debug.Print myary(x)
Next
End Sub

Sub Bar2(j() As Integer)
LetDummy j
End Sub

关于arrays - 将数组分配给属性 let 时行为不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39872663/

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