将数组分配给属性let时的行为不一致 [英] Inconsistent behavior when assigning array to a property let
本文介绍了将数组分配给属性let时的行为不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑以下代码:
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中的错误?如何解决这个错误?
When I run macro 'Foo' i get message 'internal error 51' but 'Foo2' and 'Foo3' runs fine. What's the reason for this behavior? Or is it simply a bug in VBA? How to fix this error?
背景:在我的应用程序中,我想将作为函数参数提供的数组分配给array类型的属性.
Background: In my application I want to assign array provided as function argument to property of type array.
推荐答案
对我来说,这似乎是个错误,因为错误51指出如果可以,请联系MS.
It looks like a bug to me since Error 51 says to contact MS if you get it.
如果我创建一个局部变量,则将其赋值,然后将其传递似乎可行.
If I create a local variable, assign it, then pass it along it seems to work.
非常hacky.
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
这篇关于将数组分配给属性let时的行为不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文