I am looking at VB6 code and I see a statement as follows -
Public Sub CheckXYZ(abc As Integer)
If abc <> pqr Then SetVars abc
And when I click on go to definition on SetVars, I am taken to the following definition-
Private Sub SetVars(i As Integer)
I am new to VB. Is this something that is common in VB, to allow function calls without the paranthesis?
As Ryan has pointed out, parentheses should only be used when calling a function that will return a value.
One pitfall I would like to add is that if you actually DO use parenteses unintentionally when calling a Sub, VB6 will pass the parameter by value instead of by reference.
When the Sub takes more than one parameter, this is not a risk, since this is an illegal syntax in VB6:
SomeFunc (arg1, arg2)
But consider this example:
Sub AddOne(ByRef i As Integer)
i = i + 1
End Sub
Sub Command1_Click()
Dim i as Integer
i = 1
AddOne i 'i will be passed by reference and increased by 1
Msgbox i 'Will print "2"
AddOne (i) 'i will be passed by value, so the return value will be lost!!
MsgBox i 'Will still print "2"!!
End Sub
So be aware of how you use the parentheses, a small change may have unexpected effect.
This is a feature of VB6 (one which gladly was taken away in VB.NET) and is legal syntax.
However I would not recommend using it because it can make the code more difficult to read and as @GTG pointed out can force ByVal when the method declaration is ByRef if you are not careful.
(See the MS Documentation about this here)
As such my advice is to always use the parentheses. If you see a space between the method name and the first bracket like this:
SomeSubName (abc)
This alerts you to the fact that something is wrong (i.e. abc if being forced to be passed ByVal
) so you need to use Call
and the space will be removed:
Call SomeSubName(abc)
This makes all your method calls consistent within your code.
In the rare circumstances where you want to force abc
to be passed ByVal
you can do this which makes it much more obvious:
Call SomeSubName((abc))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With