Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

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.

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.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top