PC Review


Reply
Thread Tools Rate Thread

differences in declaration and instantiation coding

 
 
Mark Kamoski
Guest
Posts: n/a
 
      3rd Sep 2003


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




 
Reply With Quote
 
 
 
 
Armin Zingler
Guest
Posts: n/a
 
      3rd Sep 2003
"Mark Kamoski" <(E-Mail Removed)> schrieb
> 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. I usually use version 3 because my declarations are
always at the start of my procedures whereas the executable statements
follow them. Also, the order of declarations shouldn't matter - that's not
true anymore for version 1 and 2.


--
Armin

 
Reply With Quote
 
Steve C. Orr, MCSD
Guest
Posts: n/a
 
      3rd Sep 2003
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.

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
Hire top-notch developers at http://www.able-consulting.com



"Mark Kamoski" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
>
> 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
>
>
>
>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      3rd Sep 2003
Hello,

"Mark Kamoski" <(E-Mail Removed)> schrieb:
> 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
--
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet


 
Reply With Quote
 
Patrick Steele [MVP]
Guest
Posts: n/a
 
      3rd Sep 2003
In article <(E-Mail Removed)>, (E-Mail Removed)
says...
>
>
> 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.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      3rd Sep 2003
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

"Mark Kamoski" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
>
> 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
>
>
>
>



 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      3rd Sep 2003
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.

"Mark Kamoski" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
>
> 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
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare and highlight differences in 2 worksheets in same workbookand list differences in 3rd worksheet Joshua Houck Microsoft Excel Programming 21 19th Aug 2011 12:21 PM
Autoadjust range declaration coding when inserting rows JW73 Microsoft Excel Programming 4 27th Apr 2010 09:50 PM
Dynamic Instantiation =?Utf-8?B?RmFyaWJh?= Microsoft C# .NET 6 4th Jun 2006 07:05 AM
Instantiation Tommy Malone Microsoft VB .NET 0 20th May 2004 04:44 PM
differences in declaration and instantiation coding Mark Kamoski Microsoft ASP .NET 5 3rd Sep 2003 06:18 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:58 PM.