【问题标题】:Visual Basic 6.0 Passing by reference problemVisual Basic 6.0 通过引用传递问题
【发布时间】:2009-07-08 17:54:44
【问题描述】:

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

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

【问题讨论】:

    标签: vb6 pass-by-reference


    【解决方案1】:

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

    Dim i, j As Integer
    

    它在编译器中读取为

    Dim i As Variant, j As Integer
    

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

    Dim i As Integer
    Dim j As Integer
    

    或者单行,一个拉:

    Dim i As Integer, j As Integer
    

    【讨论】:

      【解决方案2】:

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

      这是描述行为的article

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-09
        • 2011-03-14
        • 2018-02-13
        • 1970-01-01
        • 2012-09-06
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        相关资源
        最近更新 更多