Call关键字在VB6中有什么作用?

时间:2023-02-07 00:43:54

There's some code in our project that looks a bit like this:

我们的项目中有一些代码看起来有点像这样:

Private Sub Method1()
    Call InnerMethod
End Sub

Private Sub Method2()
    InnerMethod
End Sub

Private Sub InnerMethod()
    '' stuff
End Sub

What's the advantage of doing Method1 over Method2?

Method2优于Method2的优势是什么?

5 个解决方案

#1


From the MSDN:

来自MSDN:

You are not required to use the Call keyword when calling a procedure. However, if you use the Call keyword to call a procedure that requires arguments, argumentlist must be enclosed in parentheses. If you omit the Call keyword, you also must omit the parentheses around argumentlist. If you use either Call syntax to call any intrinsic or user-defined function, the function's return value is discarded.

调用过程时,不需要使用Call关键字。但是,如果使用Call关键字调用需要参数的过程,则必须将参数列表括在括号中。如果省略Call关键字,则还必须省略argumentlist周围的括号。如果使用Call语法调用任何内部函数或用户定义函数,则会丢弃函数的返回值。

For example:

Sub Proc1()
    Debug.Print "Hello World"
End Sub

Sub Proc2(text As String)
    Debug.Print "Hello " & text
End Sub

In the immediate window, if you enter

在即时窗口中,如果您输入

Proc1

then "Hello World" prints. If you enter

然后打印“Hello World”。如果你输入

Call Proc1

then "Hello World" prints. If you enter

然后打印“Hello World”。如果你输入

Proc2 "World"

then "Hello World" prints. If you enter

然后打印“Hello World”。如果你输入

Call Proc2 "World" 

you get a compile error. You would have to enter

你得到一个编译错误。你必须输入

Call Proc2("World")

#2


Call does nothing special other than call the method. It is a hang over from the old days of Basic when all lines had to start with a keyword. "Let" is another of these keywords, which was always put before an assignment, but is no longer required.

除了调用方法之外,调用没有什么特别之处。当所有行都必须以关键字开头时,它就是Basic的旧时代。 “让”是这些关键字中的另一个,它总是放在作业之前,但不再需要。

Method1 and Method2 do the exact same thing.

Method1和Method2完全相同。

#3


I have found a major difference about 'call' keyword with functions that having, ByRef Arguments (I have found this in MS-Access VBA editor). If you are calling the function without 'Call' keyword, ByRef aruments will not set for the calle. For Ex:

我发现'call'关键字的主要区别在于具有ByRef Arguments的函数(我在MS-Access VBA编辑器中找到了这个)。如果您在没有“Call”关键字的情况下调用该函数,则不会为calle设置ByRef aruments。对于Ex:

Private Function Test(Optional ByRef refArg As String) As Boolean    
    refArg = "Test"
    Test = True
End Function

If you call the function without the Call keyword like

如果你调用没有Call关键字的函数

Dim a As String
Test(a)

a will be an empty string, after the call returns

在调用返回后,a将是一个空字符串

If you call the function with the Call keyword like

如果你使用Call关键字调用该函数

Dim a As String
Call Test(a)

a will contain the string Test

a将包含字符串Test

The detailed explanation provided in the following link: http://blogs.msdn.com/b/ericlippert/archive/2003/09/15/52996.aspx

详细说明见以下链接:http://blogs.msdn.com/b/ericlippert/archive/2003/09/15/52996.aspx

#4


There's no difference.

没有区别。

#5


Here's a post which describes when you need to use call vs not using it and when to parentheses around your parameters.

这是一篇文章,描述了何时需要使用call而不是使用call以及何时使用括号参数。

You can also read more about call from MSDN. Essentially the main difference is that when you use call to call a function you can't access the return value.

您还可以阅读有关MSDN呼叫的更多信息。本质上主要区别在于,当您使用call调用函数时,您无法访问返回值。

#1


From the MSDN:

来自MSDN:

You are not required to use the Call keyword when calling a procedure. However, if you use the Call keyword to call a procedure that requires arguments, argumentlist must be enclosed in parentheses. If you omit the Call keyword, you also must omit the parentheses around argumentlist. If you use either Call syntax to call any intrinsic or user-defined function, the function's return value is discarded.

调用过程时,不需要使用Call关键字。但是,如果使用Call关键字调用需要参数的过程,则必须将参数列表括在括号中。如果省略Call关键字,则还必须省略argumentlist周围的括号。如果使用Call语法调用任何内部函数或用户定义函数,则会丢弃函数的返回值。

For example:

Sub Proc1()
    Debug.Print "Hello World"
End Sub

Sub Proc2(text As String)
    Debug.Print "Hello " & text
End Sub

In the immediate window, if you enter

在即时窗口中,如果您输入

Proc1

then "Hello World" prints. If you enter

然后打印“Hello World”。如果你输入

Call Proc1

then "Hello World" prints. If you enter

然后打印“Hello World”。如果你输入

Proc2 "World"

then "Hello World" prints. If you enter

然后打印“Hello World”。如果你输入

Call Proc2 "World" 

you get a compile error. You would have to enter

你得到一个编译错误。你必须输入

Call Proc2("World")

#2


Call does nothing special other than call the method. It is a hang over from the old days of Basic when all lines had to start with a keyword. "Let" is another of these keywords, which was always put before an assignment, but is no longer required.

除了调用方法之外,调用没有什么特别之处。当所有行都必须以关键字开头时,它就是Basic的旧时代。 “让”是这些关键字中的另一个,它总是放在作业之前,但不再需要。

Method1 and Method2 do the exact same thing.

Method1和Method2完全相同。

#3


I have found a major difference about 'call' keyword with functions that having, ByRef Arguments (I have found this in MS-Access VBA editor). If you are calling the function without 'Call' keyword, ByRef aruments will not set for the calle. For Ex:

我发现'call'关键字的主要区别在于具有ByRef Arguments的函数(我在MS-Access VBA编辑器中找到了这个)。如果您在没有“Call”关键字的情况下调用该函数,则不会为calle设置ByRef aruments。对于Ex:

Private Function Test(Optional ByRef refArg As String) As Boolean    
    refArg = "Test"
    Test = True
End Function

If you call the function without the Call keyword like

如果你调用没有Call关键字的函数

Dim a As String
Test(a)

a will be an empty string, after the call returns

在调用返回后,a将是一个空字符串

If you call the function with the Call keyword like

如果你使用Call关键字调用该函数

Dim a As String
Call Test(a)

a will contain the string Test

a将包含字符串Test

The detailed explanation provided in the following link: http://blogs.msdn.com/b/ericlippert/archive/2003/09/15/52996.aspx

详细说明见以下链接:http://blogs.msdn.com/b/ericlippert/archive/2003/09/15/52996.aspx

#4


There's no difference.

没有区别。

#5


Here's a post which describes when you need to use call vs not using it and when to parentheses around your parameters.

这是一篇文章,描述了何时需要使用call而不是使用call以及何时使用括号参数。

You can also read more about call from MSDN. Essentially the main difference is that when you use call to call a function you can't access the return value.

您还可以阅读有关MSDN呼叫的更多信息。本质上主要区别在于,当您使用call调用函数时,您无法访问返回值。