When to use ( )

  • Thread starter Thread starter Rob
  • Start date Start date
R

Rob

In code, I "appear" to see contradictions...

Dim aButton as New Button()

as well as

Dim aButton as New Button

Why are the ( ) present in one case, but not the other ?
 
In code, I "appear" to see contradictions...

Dim aButton as New Button()

as well as

Dim aButton as New Button

Why are the ( ) present in one case, but not the other ?

Visual Basic does not enforce strict parentheses rules like other
languages such as C# or C++. If they are absent but required the
compiler will add them for you.

Thanks,

Seth Rowe
 
Thanks...

However... I would like to know the underlying rule... when does the
complier require that they be present ?

Do certain circumstances dictate their presence ?
 
Thanks...

However... I would like to know the underlying rule... when does the
complier require that they be present ?

Do certain circumstances dictate their presence ?

Any time you call a method the parentheses are required - this
includes object instantiation.

Thanks,

Seth Rowe
 
In code, I "appear" to see contradictions...

Dim aButton as New Button()

as well as

Dim aButton as New Button

Why are the ( ) present in one case, but not the other ?

Hi,

You must use () with the NEW reserved keyword when the constructor you
are calling requires one or more parameters. For example:

Dim myString As New String("AAA")

It sets myString = "AAA".

Cheers
 
Actually, VB does not add them for instantiation via parameterless
constructors (i.e., "x = New Foo" is left as is), but VB does add them for
paremeterless method calls. I'm not sure for the reason behind this
inconsistency.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
C++ to C# Converter: converts C++ to C#
Instant C++: converts C# or VB to C++/CLI
 
Actually, VB does not add them for instantiation via parameterless
constructors (i.e., "x = New Foo" is left as is), but VB does add them for
paremeterless method calls. I'm not sure for the reason behind this
inconsistency.

Maybe because in an instantiation context you could confuse it for an
array (if you don't remember that curly braces are required)?


Mattias
 
Rob,
In addition to the other comments.

They are allowed here as they are optional.

They are optional to avoid the problem of deleting new from your first
expression & winding up with an array:

Dim aButton as Button()

aButton is now an array of buttons, probably not what you intended.
 
Back
Top