2009-07-08 50 views
1

在下面的代碼中,我得到編譯時錯誤,因爲i被視爲變體。錯誤是:「ByRef參數類型不匹配」。Visual Basic 6.0通過值傳遞參考差異

但是,如果我通過參數ByVal,爲什麼沒有錯誤?

Private Sub Command2_Click() 
    Dim i, j As Integer 
    i = 5 
    j = 7 
    Call Swap(i, j) 
End Sub 

Public Sub Swap(ByRef X As Integer, ByRef Y As Integer) 
    Dim tmp As Integer 
    tmp = X 
    X = Y 
    Y = tmp 
End Sub 

回答

3

ByVal將變體自動轉換爲整數,因爲它傳遞的是一個值。而ByRef試圖傳遞一個可以在子例程中修改的變量。實質上,我是ByRef場景中的X. VB6不允許您將變體修改爲整數。

6

當你在一行上昏暗幾個變量,即Dim i, j as Integer j變暗爲整數,但我是一個變體。你需要明確地聲明每個變量類型。我更喜歡每行只包含一個變量。

Dim i As Integer, j As Integer 

Dim i As Integer 
Dim j As Integer 

這事時,我繼承了其他程序員的代碼中,我學會了