2

在以下代码中,我收到编译时错误:

ByRef Argument type mismatch. 

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

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
4

2 回答 2

9

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

Dim i, j As Integer

它向编译器读取为

Dim i As Variant, j As Integer

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

Dim i As Integer
Dim j As Integer

或者在单行上,a la:

Dim i As Integer, j As Integer
于 2009-07-08T17:57:16.460 回答
3

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

这是描述该行为的文章。

于 2009-07-08T17:57:53.903 回答