Why can I invoke functions without parentheses in VB.NET?

3

In C #, this code does not compile:

public static string Teste(){
    string val = "";
    return val.Trim;
}

Because the Teste function requires a return of type string , and Trim is a MethodGroup . That makes perfect sense in my opinion.

However, in VB.NET, this function works perfectly:

Public Function Teste() as String
    Dim valor as String = ""
    Return valor.Trim
End Function

Why invocation of functions in VB does not need parentheses? Can I pass parameters to functions invoked in this way? After all, is there a difference between invoking with or without parentheses?

    
asked by anonymous 21.07.2017 / 14:47

1 answer

3

I do not know if anyone will be able to answer something better than: it is so because it was defined as such. Anyone who developed the language thought it was a good idea and decided that it would be done soon.

  

Can I pass parameters to functions invoked this way?

No. Parentheses are only optional for functions that do not require parameters (both for declaring and for invoking).

Sub Teste           ' Ok, não tem parâmetros, não precisa de parênteses
Sub Teste()         ' Também é correto.
Sub Te(x as String) ' Precisa de parênteses, por causa dos parâmetros

obj.Teste           ' Ok, não tem parâmetros, não precisa de parênteses
obj.Teste()         ' Mas você pode usálos, de qualquer maneira
obj.Te("A")         ' Precisa de parênteses para passar o parâmetro
  

After all, is there a difference between invoking with or without parentheses?

None. This is purely syntax.

    
21.07.2017 / 14:58