how to create class instance?

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é
 
N

Noodle

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.
 
H

Herfried K. Wagner [MVP]

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.
 
M

Mike Hofer

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.)
 
A

André

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top