我应该在 VB/VBA 中使用 Call 关键字吗? [英] Should I use Call keyword in VB/VBA?

查看:32
本文介绍了我应该在 VB/VBA 中使用 Call 关键字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 VB/VBA 中调用 subs 时使用 Call 关键字.我知道它是可选的,但是使用它还是放弃它更好?我一直认为它更明确,但也许只是噪音.

I use the Call keyword when calling subs in VB/VBA. I know it's optional, but is it better to use it or leave it off? I've always thought it was more explicit, but maybe it's just noise.

另外,我在另一个论坛上读到这个:使用 Call 关键字更快,因为它知道它不会返回任何值,所以它不需要设置任何堆栈空间来为返回值留出空间.

Also, I read this on another forum: Using the Call keyword is faster because it knows that it is not going to return any values, so it doesn't need to set up any stackspace to make room for the return value.

推荐答案

啊哈.我一直对此感到疑惑,甚至阅读一本两英寸厚的关于 VBA 的书基本上都说不要使用它,除非您想使用 VBE 的查找功能来轻松查找大型项目中的调用.

Ah ha. I have long wondered about this and even reading a two inch thick book on VBA basically says don't use it unless you want to use the Find feature of the VBE to easily find calls in large projects.

但我刚刚发现了另一个用途.

But I just found another use.

我们知道可以用冒号字符连接代码行,例如:

We know that it's possible to concatenate lines of code with the colon character, for example:

Function Test(mode as Boolean) 
    if mode = True then x = x + 1 : Exit Sub
    y = y - 1
End Sub

但是,如果您在行首使用过程调用执行此操作,则 VBE 假定您指的是标签并删除任何缩进,将行与左边距对齐(即使该过程按预期调用):

But if you do this with procedure calls at the beginning of a line, the VBE assumes that you're referring to a label and removes any indents, aligning the line to the left margin (even though the procedure is called as intended):

Function Test()
Function1 : Function2
End Function

使用 Call 语句允许在保持代码缩进的同时串联过程调用:

Using the Call statement allows concatenation of procedure calls while maintaining your code indents:

Function Test()
    Call Function1 : Call Function2
End Function

如果你在上面的例子中不使用Call语句,VBE会假设Function1"是一个标签,并在代码窗口中左对齐,即使它不会导致错误.

If you don't use the Call statement in the above example, the VBE will assume that "Function1" is an label and left align it in the code window, even though it won't cause an error.

这篇关于我应该在 VB/VBA 中使用 Call 关键字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆