以下代码在标记为行的办公室中的两台计算机上使用VB.exe中的“未处理的异常故障”杀死VB6(sp6)。
''# Form1.frm
Option Explicit
Private ArrayHolder As Class2
Private Sub Command1_Click()
Set ArrayHolder = New Class2
Dim arr(3) As Long
arr(0) = 1
arr(1) = 2
arr(2) = 3
ArrayHolder.Add arr
End Sub
''# -----------------------------------------------------------
''# Class1.cls
Option Explicit
Private m_myArray() As Long
Public Property Get myArray() As Long()
myArray = m_myArray
End Property
Friend Property Let myArray(ByRef anArray() As Long)
m_myArray = anArray
End Property
''# -----------------------------------------------------------
''# Class2.cls
Option Explicit
Friend Function Add(newArray() As Long) As Class1
Dim oClass As Class1
Set oClass = New Class1
oClass.myArray = newArray <- This kills VB6 dead
MsgBox "passed"
End Function
从我在各种网站上可以看出,我正确地传递了一个数组,但我实际上是否正确地做了,为什么它会导致VB6死得如此可怕?
答案 0 :(得分:8)
我没有答案,当然是一个奇怪的问题,但为什么不只是添加方法并继续前进?
'Passed the test
Public Sub LetMyArray(anArray() As Long)
m_myArray = anArray
End Sub
'oClass.MyArray = newArray ' <- This kills VB6 dead
oClass.LetMyArray newArray ' <- This works
答案 1 :(得分:8)
这是MS中从未修复过的错误(已编译好)。我正在使用像这样的临时数组的解决方法:
Friend Function Add(newArray() As Long) As Class1
Dim oClass As Class1
Dim tempArray() As Long
Set oClass = New Class1
tempArray = newArray
oClass.myArray = tempArray <- Should work now
MsgBox "passed"
End Function
仅供参考,当您不小心将鼠标悬停在param或数组属性上时,Byte数组(长数组是安全的)会变得更糟。最好让你的鼠标远离代码: - ))
答案 2 :(得分:1)
我不知道为什么会这样,但如果你想要一个数组的副本,请在class2中使用Variant:
Private m_myArray As Variant
Public Property Get myArray() As Variant
myArray = m_myArray
End Property
Friend Property Let myArray(anArray As Variant)
m_myArray = anArray
End Property
修复它,但我仍然对这个原因感到好奇。