Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.net - "Dim x as new Y()" vs "Dim x as Y = new Y()"

Tags:

vb.net

In our VB.net codebase I occasionally see Dim x as new Y(), where Y is a class.

What is this code doing and how does it differ to the more common Dim x as Y = new Y()"?

Thanks.

like image 609
Brian Beckett Avatar asked Dec 16 '10 19:12

Brian Beckett


2 Answers

They are exactly the same; the first is just a shortcut:

http://msdn.microsoft.com/en-us/library/Aa903373

like image 88
GendoIkari Avatar answered Sep 20 '22 07:09

GendoIkari


As mentioned in @Gendolkari's answer, the code is exactly the same. But I dispute that Dim x As Y = new Y() is more common. I've seen a lot of code from a lot of different places, and Dim x As New Y() is a very commonly used shortcut.

However, it might be more common at your work place. If that's the case, I have to come down against using the shortcut. Coding standards are important. If used correctly, even simple deviations like this are valuable because they set up easy to spot red flags that the code may need deeper inspection for other flaws.

like image 26
Joel Coehoorn Avatar answered Sep 19 '22 07:09

Joel Coehoorn