What does the Call keyword do in VB6?

ghz 7months ago ⋅ 104 views

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?

Answers

In VB6, the difference between using Call and directly calling a method without Call is mostly syntactical, but there are some nuances to consider:

Using Call

Private Sub Method1()
    Call InnerMethod
End Sub

Direct Call

Private Sub Method2()
    InnerMethod
End Sub

Differences and Considerations

  1. Syntax and Readability:

    • Call Keyword:
      • Using Call explicitly indicates that a procedure or function is being invoked. This can make the code more readable for those who are accustomed to this syntax or are coming from other languages that use similar constructs.
      • It can make it clearer that no assignment is happening, which can be useful in distinguishing function calls from assignments when the function returns a value.
    • Direct Call:
      • Directly calling the method without Call is more concise and is generally the preferred modern practice in VB6.
      • It makes the code cleaner and easier to read by reducing unnecessary keywords.
  2. Return Values:

    • When calling a function that returns a value, using Call ignores the return value. Without Call, you can assign the return value to a variable.
    Dim result As Integer
    result = SomeFunction() ' Direct call with assignment
    Call SomeFunction() ' Call ignores the return value
    
    • For a subroutine (which does not return a value), there is no difference in behavior between using Call and a direct call.
  3. Historical Context:

    • The Call keyword is a carryover from earlier versions of BASIC and VB. It was more commonly used in older codebases.
    • Modern VB6 coding standards generally prefer the more concise syntax without Call.
  4. Consistency:

    • If the codebase uses Call consistently, it might be beneficial to continue using it for the sake of consistency and maintaining a uniform coding style.
    • If the project does not use Call elsewhere, omitting it would be more consistent with the rest of the code.

Best Practices

  • For Subroutines: Prefer the direct method call without Call for simplicity and readability, unless there is a specific reason to use Call.
  • For Functions: Use direct calls and handle return values appropriately. Only use Call when you explicitly want to ignore the return value of a function.

Conclusion

The choice between using Call and a direct call in VB6 largely comes down to personal or team preference, readability, and consistency within the codebase. In most modern VB6 code, the direct call is preferred for its simplicity and conciseness. There is no significant performance difference between the two approaches.