Visual Basic 6.0通过引用问题传递

dha*_*0us 2 vb6 pass-by-reference

在下面的代码中,我得到一个编译时错误:

ByRef Argument type mismatch. 
Run Code Online (Sandbox Code Playgroud)

但是,如果我将i,j的声明更改为:

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

错误消失了.为什么?

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)

Joh*_*udy 9

这是因为当您在VB6中执行此操作时:

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

它读取编译器为

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

导致您的类型不匹配.正如你所说,答案是用类型声明两者,如你的代码:

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

或者在一条线上,一个la:

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