When do I need "As New" when instantiating object?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

So I'll type something like:

Dim cmd As New SqlCommand()(

But for some things, it doesn't like "As New" and wants me to type like:

Dim nod As employeeNode

When do I know when to use "As New" as opposed to not? Intellisense always
sets me straight, but I'd like to understand better.

Thanks,
Ron
 
As New is a shortcut for ...

Dim cmd as SqlCommand = new SqlCommand();

which declares then instantiates a new SqlCommand .. for some things it
doesn't make sense to do this.

Make sense?

Cheers,

Greg
 
Ron said:
So I'll type something like:

Dim cmd As New SqlCommand()(

But for some things, it doesn't like "As New" and wants me to type like:

Dim nod As employeeNode

When do I know when to use "As New" as opposed to not? Intellisense always
sets me straight, but I'd like to understand better.

Thanks,
Ron

You cannot use As New if the class doesn't have a public constructor.

Adam Ruth
 
The other case being you need to use a constructor other than the default,
like:
Dim cmd As New SqlCommand("SELECT * FROM MyTable")
 
Back
Top