Is it possible to call a VB function without the parenthesis?

前端 未结 4 392
-上瘾入骨i
-上瘾入骨i 2021-01-03 07:17

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
<         


        
相关标签:
4条回答
  • 2021-01-03 08:06

    Sub calls with parameters do not require the parenthesis. Paranthesis are only required for a function returning a result.

    Private Sub Testy1()
        Function1 "Testy2" ' does not require parenthesis
        Debug.Print Function1("Testy3") ' does require parenthesis
    End Sub
    
    Private Function Function1(str as String) as Boolean
        Function1 = True
    End Function
    
    0 讨论(0)
  • 2021-01-03 08:15

    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))

    0 讨论(0)
  • 2021-01-03 08:18

    I don't personally use or recommend the use of the Call statement. I find personally that the parens with functions and lack thereof with subs differentiate the two effectively. That said, I really like Matt's use of the call statement/byval/double parens. In the rare circumstances when you want this, it makes it stand out even MORE from every other call if it's the only place where you use the Call statement. I'm gonna use that, if I live long enough to need to. :)

    0 讨论(0)
  • 2021-01-03 08:19

    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.

    0 讨论(0)
提交回复
热议问题