簡體   English   中英

在 Visual Basic 中將數組作為參數傳遞會導致 Excel 崩潰

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

這是產生問題的最少代碼量。 我有一個模塊和兩個類(“Test”和“Test2”)。 在 Module1 中,我有這個 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

在 class 測試中,我有這個:

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

最后在 class Test2 中,我有:

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

運行 Sub testf將導致 Excel 關閉並重新打開。 我沒有彈出窗口告訴我存在運行時錯誤,所以我不知道錯誤是什么。 first到達,因此問題在於如何調用second 嘗試first進行錯誤處理並沒有幫助。 例如,在t.second = s之前放置一個“on error...”來獲取錯誤描述是行不通的。

如果我們將second更改為接受一個變體參數,那么這就解決了問題並且沒有崩潰。 但這並不能幫助我理解問題的根源。 t.second = s s之前打印 s 的變量類型確認它是 8200,它是一個字符串數組。 那么second如何接受它的論點有什么問題呢?

提供的代碼為我生成了 RTE 51:內部錯誤。 設置對我來說似乎很奇怪,但以下工作:

在 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

Test2測試2:

Private pSecond() As String

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM