简体   繁体   中英

Passing an array as argument in Visual Basic causes Excel to crash

Here's the minimum amount of code that produces the problem. I have a module and two classes ("Test" and "Test2"). In Module1, I have this function:

Sub testf()
    Dim s() As String
    ReDim s(0) As String
    s(0) = "value"
    Dim t As New Test
    t.first = s
End Sub

in the class Test, I have this:

Property Let first(s() As String)
    Debug.Print "first"
    Dim t As New Test2
    t.second = s
End Property

and finally in the class Test2, I have:

Property Let second(s() As String)
    Debug.Print "second"
End Property

Running the Sub testf will cause Excel to close and reopen. I don't get a popup telling me that there's a run-time error, so I don't know what the error is. It reaches first and, so, the issue is with how second is being called. Attempting to do error handling within first doesn't help. For example, placing an "on error..." preceding t.second = s to get the error description doesn't work.

If we change second to accept a variant argument instead, then this solves the problem and there is no crash. But that doesn't help me understand what the issue was to begin with. Printing the variable type of s before t.second = s confirms that it's 8200, which is a string array. So then what's the problem with how second accepts its argument?

The provided code produced a RTE 51: Internal Error for me. The setup seems odd to me, but the following works:

In class Test :

Private pFirst() As String

Property Let first(s() As String)
    Debug.Print "first"
    pFirst = s
    Dim t As New Test2
    t.second = pFirst
End Property

In class Test2 :

Private pSecond() As String

Property Let second(s() As String)
    Debug.Print "second"
    pSecond = s
End Property

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM