Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
I make a lot of small programs in VB.Net. Of course, I have to declare variables. The way I declare "New" variables is
VB
Public House As Building = New Building("live")

Is that the same as this?
VB
Public House As New Building("live")

If not, what's faster, does it matter, what is safer?

Btw, I made the house idea while I was typing this.
Posted
Updated 22-Apr-12 13:29pm
v2
Comments
Sergey Alexandrovich Kryukov 22-Apr-12 20:05pm    
Not so interesting question. Why not simply using the disassembler to figure it out? It would be faster than asking this question and waiting for the answer.
--SA
VJ Reddy 22-Apr-12 20:11pm    
Exactly. We thought same way.
I saw your comment after posting my answer.

Let us see
VB
Sub Main
	Dim House As Building = New Building("live")
	Dim House2 As New Building("live")
End Sub

Public class Building
	Public sub New (Name as string)
		
	end sub
End class

The IL generated for the above code is
VB
IL_0001:  ldstr       "live"
IL_0006:  newobj      UserQuery+Building..ctor
IL_000B:  stloc.0
IL_000C:  ldstr       "live"
IL_0011:  newobj      UserQuery+Building..ctor
IL_0016:  stloc.1

Building..ctor:
IL_0000:  nop
IL_0001:  ldarg.0
IL_0002:  call        System.Object..ctor
IL_0007:  nop
IL_0008:  nop
IL_0009:  ret


From the above it is clear that the IL is same in both cases. Hence, there is no difference in declaring in either way.
I used Dim instead of public as public is not allowed inside a method.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Apr-12 20:15pm    
Thank you for taking the labor of running it. It was more or less apparent to me the two cases are the same, simply because I see no room for different IL. So, I answered, too, before I saw you answer -- please see.
Of course, my 5.
--SA
VJ Reddy 22-Apr-12 20:18pm    
Thank you, SA.
I used LINQPad, which has a IL tab in the output pan to quickly see the IL. I too thought there will not be any difference between the statements, but if the IL is seen it would be more clear. Hence, posted IL from LINQPad.
Sergey Alexandrovich Kryukov 22-Apr-12 21:15pm    
I appreciate the idea of LINQPad. As I almost never work with RDBMS, I never tried it. Do you mean using this the feature quoted below?
"Aside from being useful for writing queries, LINQPad can be used to interactively write C# code without having to compile it. This expands its use to a general "test workbench" where C# code can be quickly prototyped outside of Visual Studio."
Wikipedia,
http://en.wikipedia.org/wiki/LINQPad
Is it the same as "Code Scratchpad"?

What do you think about this tool? Is it non-obtrusive and light-weight? One concern is that it is proprietary software...
--SA
VJ Reddy 22-Apr-12 22:01pm    
You are correct. LINQPad is useful as Code Scratchpad to test code snippets quickly. Even though, the LINQPad is not an open-source product as given at http://www.linqpad.net/#license, as per the license terms of the free version it can be useful for testing the code fragments.
In my experience, it is light-weight and non obtrusive.
Thank you.
Sergey Alexandrovich Kryukov 25-Apr-12 16:54pm    
Thank you very much for answering.
--SA
I don't think there can be any difference, but I don't know what was to ask about. I never install support of VB.NET projects with Visual Studio and lazy enough to test it with always available compiler via a command line, otherwise I would simply compile both cases, run compiled code through the disassembler ILDASM.EXE into IL and compared the output. Why even asking things like that?

Please see:
http://msdn.microsoft.com/en-us/library/f7dy01k1%28v=vs.100%29.aspx[^].

—SA
 
Share this answer
 
v2
Comments
VJ Reddy 22-Apr-12 20:28pm    
Good answer with an informative reference. 5!
Sergey Alexandrovich Kryukov 22-Apr-12 21:04pm    
Thank you, VJ.
Teamwork! :-)
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900