how to create class instance?

  • Thread starter Thread starter André
  • Start date Start date
A

André

Hi,

i defined a class "myclass" in the App_Code directory of my asp.net
application.

i tried both syntaxes and both work.
My question is: is there a difference between both lines and what's the
best?

dim x as new myclass
dim x as new myclass()

Thanks
André
 
Hi,

i defined a class "myclass" in the App_Code directory of my asp.net
application.

i tried both syntaxes and both work.
My question is: is there a difference between both lines and what's the
best?

dim x as new myclass
dim x as new myclass()

Thanks
André

I would say without the brackets unless you have arguments that need
to be entered in the constructor then you need the brackets.

But at the end of the day its just personal preference.
 
André said:
i defined a class "myclass" in the App_Code directory of my asp.net
application.

i tried both syntaxes and both work.
My question is: is there a difference between both lines and what's the
best?

dim x as new myclass
dim x as new myclass()


Both lines are semantically identical. Personally I prefer the second one
for consistency. VB does not enforce empty parentheses on method
(constructor) calls.
 
Both lines are semantically identical. Personally I prefer the second one
for consistency. VB does not enforce empty parentheses on method
(constructor) calls.

Agreed.

However, it's worth noting that if your class only provides a default
(parameterless) constructor, and you're using Visual Studio .NET
2002/2003, the IDE will remove the empty parentheses automatically.
It's annoying. I prefer to put them in there. (A call to the default
constructor is still, technically, a method call.)
 
Thanks

"Mike Hofer" <[email protected]> schreef in bericht
Both lines are semantically identical. Personally I prefer the second one
for consistency. VB does not enforce empty parentheses on method
(constructor) calls.

Agreed.

However, it's worth noting that if your class only provides a default
(parameterless) constructor, and you're using Visual Studio .NET
2002/2003, the IDE will remove the empty parentheses automatically.
It's annoying. I prefer to put them in there. (A call to the default
constructor is still, technically, a method call.)
 
Back
Top