VB -Parentheses! Please explain?

4.8k views Asked by At
Sub Main() 

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

End Sub

Why does Sub Main () need them? How do they apply to this procedure? .WriteLine("") here i am adding a value. Console.Read() is this holding the value "check" to show on console? Why why are they here. I know all you experts think this is a dumb question, however i can not wrap my head around it help! To me these are boxes that hold or pass a procedure value. Is sub main a container holding the code the uses enters? If so, why is it that when a form button is used it is full? But here a VB default unused and empty? Seems to me with no event values it should not be there....?????

2

There are 2 answers

2
user2864740 On

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.

3
Abraham On

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.