在 VB.NET 中调用过程的优缺点是什么?

发布于 2024-08-13 16:53:37 字数 306 浏览 6 评论 0原文

我想知道在VB.NET中使用Call关键字和不使用Call调用程序的优缺点?

Private Sub ProOne()
     ' Code Are Here
End Sub

Private Sub Calling()
   ProOne()           ' I would like to know pros and cons of this
   Call ProOne()      ' And I would like to know pros and cons of this
End Sub

预先感谢大家。

I would like to know the pros and cons of calling procedures with Call Keyword and without Call in VB.NET?

Private Sub ProOne()
     ' Code Are Here
End Sub

Private Sub Calling()
   ProOne()           ' I would like to know pros and cons of this
   Call ProOne()      ' And I would like to know pros and cons of this
End Sub

Thanks in advance all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

骑趴 2024-08-20 16:53:37

没有优点,也没有缺点。

Call 关键字是旧 VB 方言中的遗留关键字。

在VB.net中它没有任何意义,是语法糖。

There are no pros, and there are no cons.

The Call keyword is a legacy keyword from older VB dialects.

In VB.net it has no meaning, and is syntatic sugar.

凶凌 2024-08-20 16:53:37

来自此处

您通常使用 Call 语句来调用不支持的过程
返回一个值。如果过程有返回值,则 Call 语句
丢弃它。

调用时不需要使用 Call 语句
程序。但是,它提高了代码的可读性。

因此,本质上,ProOne() 和 Call ProOne() 在语义上是等效的。

From here:

You normally use the Call statement to call a procedure that does not
return a value. If the procedure returns a value, the Call statement
discards it.

You are not required to use the Call statement when calling a
procedure. However, it improves the readability of your code.

So, in essence, ProOne() and Call ProOne() are semantically equivalent.

-柠檬树下少年和吉他 2024-08-20 16:53:37

我发现的一个有趣的用途(建议R#)是当您需要创建一个实例只是为了调用时单个方法,然后将其标记为垃圾回收。
但不确定我是否保留它。

例如

Call (new MyType()).MySub()

相当于

dim anInstance = new MyType
anInstance.MySub

One interesting use I found (R# suggested), was when you need to create an instance just to call a single method and then mark it for garbage collection.
Not sure if I'm keeping it though.

For example

Call (new MyType()).MySub()

equivalent of

dim anInstance = new MyType
anInstance.MySub
缱绻入梦 2024-08-20 16:53:37

尽管它们在技术上是等效的,但我反对使用“Call”。从 VB6 迁移到 VB.Net 时,重要的是要认识到它们是完全不同的语言,必须以完全不同的方式编写。不幸的是,Microsoft 希望为 VB6 开发人员提供支持,他们通过添加模仿 VB6 功能的功能来提供此支持,但明显逊色于 .Net 等效项。

切断与任何 VB6 遗留物的所有联系将使开发人员尽快停止使用这些位,并带来更好的代码输出。

Although they are technically equivalent, I would argue against using "Call". When moving from VB6 to VB.Net, it's important to realizae that they are completely different languages that have to be written for in completely different ways. Unfortunately, Microsoft wanted to provide support for VB6 developers, and they provided this by adding functionality that mimics the VB6 functionality, but is siginificantly inferior to the .Net equivalent.

Cutting all ties with any of the VB6 holdovers will make developers stop using these bits as quickly as possible, and lead to better code output.

睫毛上残留的泪 2024-08-20 16:53:37

来自文档

将控制权转移到函数、子函数、
或动态链接库 (DLL)
程序。 [ 调用 ] 过程名称 [
(参数列表)]

所以,

您通常使用 Call 语句来
调用不返回的过程
一个值。如果该过程返回一个
值,Call 语句将其丢弃。

您无需使用呼叫
调用过程时的语句。
但它提高了可读性
您的代码。

From documentation

Transfers control to a Function, Sub,
or dynamic-link library (DLL)
procedure. [ Call ] procedureName [
(argumentList) ]

so,

You normally use the Call statement to
call a procedure that does not return
a value. If the procedure returns a
value, the Call statement discards it.

You are not required to use the Call
statement when calling a procedure.
However, it improves the readability
of your code.

迷离° 2024-08-20 16:53:37

即使在 2019 年,Call 语句仍然具有相关性:“当被调用表达式不以标识符开头时,通常会使用 Call 关键字。不建议将 Call 关键字用于其他用途。”

调用语句 (Visual Basic)

MSDN 代码示例:

Sub TestCall()
    Call (Sub() Console.Write("Hello"))()

    Call New TheClass().ShowText()
End Sub

Class TheClass
    Public Sub ShowText()
        Console.Write(" World")
    End Sub
End Class

The Call statement still has relevance, even in 2019: "You typically use the Call keyword when the called expression doesn’t start with an identifier. Use of the Call keyword for other uses isn’t recommended."

Call Statement (Visual Basic)

MSDN code sample:

Sub TestCall()
    Call (Sub() Console.Write("Hello"))()

    Call New TheClass().ShowText()
End Sub

Class TheClass
    Public Sub ShowText()
        Console.Write(" World")
    End Sub
End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文