Tutorial :What does the Call keyword do in VB6?



Question:

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?


Solution:1

From the 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.

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

Call Proc1  

then "Hello World" prints. If you enter

Proc2 "World"  

then "Hello World" prints. If you enter

Call Proc2 "World"   

you get a compile error. You would have to enter

Call Proc2("World")  


Solution:2

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.

Method1 and Method2 do the exact same thing.


Solution:3

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:

Private Function Test(Optional ByRef refArg As String) As Boolean          refArg = "Test"      Test = True  End Function  

If you call the function without the Call keyword like

Dim a As String  Test(a)  

a will be an empty string, after the call returns

If you call the function with the Call keyword like

Dim a As String  Call Test(a)  

a will contain the string Test

The detailed explanation provided in the following link: http://blogs.msdn.com/b/ericlippert/archive/2003/09/15/52996.aspx


Solution:4

There's no difference.


Solution:5

Here's a post which describes when you need to use call vs not using it and when to parentheses around your parameters.

You can also read more about call from MSDN. Essentially the main difference is that when you use call to call a function you can't access the return value.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »