VB -Parentheses! Please explain?

后端 未结 2 1538
余生分开走
余生分开走 2021-01-21 05:04
Sub Main() 

    Console.WriteLine(\"check\")   
    Console.Read()

End Sub

Why does Sub Main () need them? How do they apply to this procedure? .Wri

相关标签:
2条回答
  • 2021-01-21 05:27

    Parenthesis are required when they are required, and optional when they are optional. In the case of empty parameter/argument lists parenthesis are "just for show".

    A Sub Procedure can be declared as Sub Main() or Sub Main - the parenthesis are optional when there are no parameters. Likewise, procedures/functions can be invoked without parenthesis if (and only if) no arguments are supplied.

    Sub A               ' ok, no parameter list - no need for parenthesis
    Sub A()             ' it's fine to use parenthesis anyway
    Sub B(x as Integer) ' need parenthesis for parameter list
    
    obj.A               ' ok, no arguments - no need for parenthesis
    obj.A()             ' it's fine to use parenthesis anyway
    obj.B(42)           ' need parenthesis when arguments are specified
    

    In the above, the definitions of A and invocations of A are equivalent as the parenthesis are optional in these cases.

    0 讨论(0)
  • 2021-01-21 05:45

    When calling a method you do have a choice in VB whether you want to include the parentheses if there are no parameters. The same holds true for the definition of a method, be it a function or a sub.

    See http://msdn.microsoft.com/en-us/library/dz1z94ha.aspx (Sub Statement on MSDN).

    Calling a Sub Procedure

    You call a Sub procedure by using the procedure name in a statement and then following that name with its argument list in parentheses. You can omit the parentheses only if you don't supply any arguments. However, your code is more readable if you always include the parentheses.

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