What is pros and cons of calling procedures in VB.NET?
I would like to know the pros and cons of calling pr开发者_如何学Cocedures 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.
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.
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.
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
精彩评论