vba VB6 中的 Call 关键字有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/479891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the Call keyword do in VB6?
提问by Ant
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?
与方法 2 相比,方法 1 的优势是什么?
回答by Patrick Cuff
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 关键字,则还必须省略参数列表周围的括号。如果您使用 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")
回答by Martin Brown
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.
Call 除了调用方法没有什么特别的。当所有行都必须以关键字开头时,Basic 的旧时代已经过去了。“Let”是这些关键字中的另一个,它总是放在赋值之前,但不再需要。
Method1 and Method2 do the exact same thing.
方法 1 和方法 2 做完全相同的事情。
回答by Abraham
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:
我发现“调用”关键字与具有 ByRef 参数的函数的主要区别(我在 MS-Access VBA 编辑器中发现了这一点)。如果您在没有“Call”关键字的情况下调用函数,则不会为被调用者设置 ByRef 参数。例如:
Private Function Test(Optional ByRef refArg As String) As Boolean
refArg = "Test"
Test = True
End Function
If you call the function without the Callkeyword like
如果你调用函数时没有Call像这样的关键字
Dim a As String
Test(a)
awill be an empty string, after the call returns
a将是一个空字符串,调用返回后
If you call the function with the Callkeyword like
如果您使用Call关键字调用该函数,例如
Dim a As String
Call Test(a)
awill 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
回答by JoshBerke
Here's a postwhich describes when you need to use call vs not using it and when to parentheses around your parameters.
这是一篇文章,描述了何时需要使用 call 与不使用它以及何时在参数周围加上括号。
You can also read more about callfrom MSDN. Essentially the main difference is that when you use call to call a function you can't access the return value.
您还可以从 MSDN阅读更多有关呼叫的信息。本质上的主要区别在于,当您使用 call 调用函数时,您无法访问返回值。
回答by James Sun
There's no difference.
没有区别。

