Visual Basic 6.0通过引用问题传递

时间:2009-07-08 17:54:45

标签: vb6 pass-by-reference

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

ByRef Argument type mismatch. 

但如果我将i的声明改为:

Dim i As Integer
Dim j As Integer

错误消失了。为什么呢?

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

2 个答案:

答案 0 :(得分:9)

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

Dim i, j As Integer

它将编译器读取为

Dim i As Variant, j As Integer

导致您的类型不匹配。正如你所说的那样,答案就是在代码中声明两者:

Dim i As Integer
Dim j As Integer

或者在单行上,la:

Dim i As Integer, j As Integer

答案 1 :(得分:3)

在VB 6中,我被认为是一个变体,而不是你所描述的整数。

这是描述行为的article

相关问题