在VB6中将数组作为参数传递

gra*_*eds 10 arrays vb6 parameters

下面的代码在标记的行上的办公室中的两台机器上杀死了VB6(sp6)中的'未处理的VB.exe异常错误'.

''# 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
Run Code Online (Sandbox Code Playgroud)

从我在各种网站上可以看出,我正确地传递了一个数组,但我实际上正确地做了,为什么它导致VB6死得如此可怕?

Edu*_*eni 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
Run Code Online (Sandbox Code Playgroud)

  • 我想你刚刚在VB运行时发现了一个错误.但我们永远不会看到SP7 :) (3认同)

wqw*_*wqw 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
Run Code Online (Sandbox Code Playgroud)

仅供参考,当您不小心将鼠标悬停在param或数组属性上时,Byte数组(长数组是安全的)会变得更糟.最好让你的鼠标远离代码:-))