By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,664 Members | 1,649 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,664 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Oct 10 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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 Cook" <Ro*****@discussions.microsoft.comwrote in message
news:0D**********************************@microsof t.com...
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

Oct 10 '06 #2

P: n/a
Ron Cook wrote:
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
Oct 11 '06 #3

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

Oct 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.