Visual Basic 6.0通过引用问题传递

时间:2021-06-28 16:33:09

In the following code, I get a compile time error:

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

ByRef Argument type mismatch. 

But if I change the declaration of i,j to :

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

Dim i As Integer
Dim j As Integer

The error goes away. Why?

错误消失了。为什么?

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 个解决方案

#1


This is because when you do this in VB6:

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

Dim i, j As Integer

It reads to the compiler as

它读取编译器为

Dim i As Variant, j As Integer

Leading to your type mismatch. The answer is, as you said, to declare both with types, either as in your code:

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

Dim i As Integer
Dim j As Integer

Or on a single line, a la:

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

Dim i As Integer, j As Integer

#2


In VB 6, I is considered a variant, not an integer in the case you're describing.

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

Here's an article that describes the behavior.

这是一篇描述行为的文章。

#1


This is because when you do this in VB6:

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

Dim i, j As Integer

It reads to the compiler as

它读取编译器为

Dim i As Variant, j As Integer

Leading to your type mismatch. The answer is, as you said, to declare both with types, either as in your code:

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

Dim i As Integer
Dim j As Integer

Or on a single line, a la:

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

Dim i As Integer, j As Integer

#2


In VB 6, I is considered a variant, not an integer in the case you're describing.

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

Here's an article that describes the behavior.

这是一篇描述行为的文章。