tags:

views:

181

answers:

4

I have see code like this

Dim s as something = new something
Dim s as new something

what's the difference? is there any?

A: 

I believe all you're doing is specifically casting something as something.

Ian P
There is no cast involved in this scenario
JaredPar
+2  A: 

There is no difference. Those signatures are identical as far as VB is concerned. One has less typing though :)

JaredPar
+1 for answering the question that was asked.
Binary Worrier
+8  A: 

A slight difference.

The first allows you to do:

Dim s as ParentType = new InheritedType

The second doesn't.

The "advantage" of this is s can be a number of different types related to ParentType without it exploding at runtime.

Oli
That is not the question being asked.
JaredPar
Isn't it? I've shown the difference between the two and explained what it allows you to do.
Oli
The question asked what is the difference between "x = new x". Not "x = new y".
JaredPar
I'm just slighly on the war path today. I went through a slew of questions last night where people were being up voted for answering a completely different question than the one that was asked. Yours is on topic, I'm just benig blinded by the sheer insanity of the other questions.
JaredPar
Sure. I thought the question was regarding the two constructs rather than the specific examples given. I was trying to explain that the only reason (other than for consistency) to use the first style is so you can specify a related type.
Oli
I agree. I +1'd you for a good answer. It's just been frustrating reading answers on some other questions.
JaredPar
I'm sorry, but to answer a question while providing additional pertinent information is, in my opinion a more valuable answer than one that only answers the question as asked. Q:Is it possible to do "thing"? A:Yes, but here is some info that might make a more informed decision.
Richard B
A: 

There is no difference.

You may see some developers that prefer

Dim s as something = new something

over

Dim s as new something

This is probably a hold over from Vb6 (as new in VB6 does more that you'd think and has nasty side effects). It's one of the things that was "fixed" with Vb.Net

Just to reiterate, in Vb.Net the statements are Equivalent i.e. both statements will build exactly the same IL.

Binary Worrier