Visual Basic 6.0传递值参考差异

dha*_*0us 1 vb6 pass-by-reference pass-by-value

在下面的代码中,我得到一个编译时错误,因为i它被视为一个变体.错误是:"ByRef Argument type mismatch.".

但是,如果我传递参数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
Run Code Online (Sandbox Code Playgroud)

jac*_*jac 6

当你在一行上变暗几个变量时,即Dim i, j as Integerj被变暗为整数,但我是变体.您需要显式声明每个变量类型.我更喜欢每行只包含一个变量.

Dim i As Integer, j As Integer
Run Code Online (Sandbox Code Playgroud)

要么

Dim i As Integer
Dim j As Integer
Run Code Online (Sandbox Code Playgroud)

这是我在继承另一个程序员代码时学到的东西