differences in declaration and instantiation coding

M

Mark Kamoski

Hi Everyone--

Please help.

What (really) is the difference between these 3 code snippets?.

'1
Dim objData1 As New DataSet()

'2
Dim objdata2 As DataSet = New DataSet()

'3
Dim objdata3 As DataSet
objdata3 = New DataSet()

Which is fastest?

Is there ANY difference between them?

Which do you prefer?

Which do you suggest?

What are the factors involved?

Please advise.

Thank you very much.

--Mark
 
S

Steve C. Orr, MCSD

Logically speaking, they are all exactly the same.
I'm relatively sure the first two would compile to the exact same IL. The
third one might also.
You could always use ILDASM to be sure.
I wouldn't expect any significant performance difference between them. You
could put them in a loop 1000 times and time them to be sure if you care
that much.
Personally I prefer your first example, it's short, simple, descriptive and
to the point.
 
H

Herfried K. Wagner [MVP]

Hello,

Mark Kamoski said:
What (really) is the difference between these 3 code
snippets?.

'1
Dim objData1 As New DataSet()

'2
Dim objdata2 As DataSet = New DataSet()

'3
Dim objdata3 As DataSet
objdata3 = New DataSet()

Which is fastest?

Is there ANY difference between them?

Which do you prefer?

Which do you suggest?

What are the factors involved?

There is no difference.

HTH,
Herfried K. Wagner
 
P

Patrick Steele [MVP]

Hi Everyone--

Please help.

What (really) is the difference between these 3 code snippets?.

'1
Dim objData1 As New DataSet()

'2
Dim objdata2 As DataSet = New DataSet()

'3
Dim objdata3 As DataSet
objdata3 = New DataSet()

Which is fastest?

Is there ANY difference between them?

Which do you prefer?

Which do you suggest?

What are the factors involved?

In VB.NET, there is no difference with any of those code snippets. They
all get compiled into the exact same IL.
 
J

Jay B. Harlow [MVP - Outlook]

Mark,
In addition to the others comments.
Which is fastest?
I would expect the first two, in that the third one implicitly initializes
the variable to Nothing, then you set it to a value.

This may be more of an issue with Value Types over Reference types.

However the time difference is going to be so extremely minute, that I don't
actually worry about it. Also I would expect the JIT compiler to optimize
the difference in Release builds.

In other words they all would be equally fast.
Which do you prefer?
I prefer the first one, unless I need the second one or third one.
Which do you suggest?
I suggest using the first one, unless you need the second one or third one.
What are the factors involved?
The first one is my first choice.

The second one is needed when you are defining a variable of a base class,
but you are initializing it with a value of a derived class or from a
Factory Method someplace.

Dim arg As String
Dim f As BaseForm = BaseForm.CreateForm(arg)

Where the MainForm.CreateForm method is a Shared Function (a Factory Method)
that based on the arg, is able to create a handful of different forms all
derived from BaseForm.

Dim frm As Form = New MainForm()

Where MainForm is derived from Form, but I really do not want a MainForm
variable here. I don't use this very often, but once in a while, when I want
to make sure the routine only operates on the base class members... (read I
am attempting to limit coupling to the derived class).

The third one is needed when your initialization needs to be done in a
try/catch, while the variable needs to be available in the Finally block.

Dim con as SqlConnection
Try
con = New SqlConnection
Finally
If Not con Is Nothing Then
con.Close()
End If
End Try

Hope this helps
Jay
 
K

Kevin Spencer

I would suspect that both 1 and 2 would run slightly faster, as they are
shortcuts, and are a single instruction, whereas the third is 2
instructions. However, the compiler might just be smart enough to compile
all 3 to the same IL code.

As to the factors involved, make sure you understand what each is doing. I
have seen green developers use Number 1 or Number 2 when there was no need
to instantiate the object (the object was instantiated later as a result of
some other instruction), which is a waste of processor.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top