473,386 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

differences in declaration and instantiation coding



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


Nov 17 '05 #1
5 1311
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" <mk******@yahoo.com> wrote in message
news:OH**************@tk2msftngp13.phx.gbl...


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

Nov 17 '05 #2
Hello,

"Mark Kamoski" <mk******@yahoo.com> 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
Nov 17 '05 #3
In article <OH**************@tk2msftngp13.phx.gbl>, mk******@yahoo.com
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
Nov 17 '05 #4
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" <mk******@yahoo.com> wrote in message
news:OH**************@tk2msftngp13.phx.gbl...

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

Nov 17 '05 #5
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" <mk******@yahoo.com> wrote in message
news:OH**************@tk2msftngp13.phx.gbl...


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

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sylvain Thenault | last post by:
Hi there ! I've noticed the following problem with python >= 2.3 (actually 2.3.4 and 2.4): syt@musca:test$ python Python 2.3.4 (#2, Sep 24 2004, 08:39:09) on linux2 Type "help", "copyright",...
6
by: Andrew Ward | last post by:
Hi All, Could someone please tell me why this code does not compile. struct A {}; struct B { B(A &) {} }; struct C {
6
by: blueblueblue2005 | last post by:
Hi, below is the code I copy from c++ tutorial, it is about template specialization, but when I compile, I got error message: error: template-id `module<>' for `int mypair<int>::module()' does...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
7
by: Peter Merwood | last post by:
Hi I've been coding in VB for a good number of years and have recently made the switch to VB.NET. When coding I put all my declaration/Dim statements at the beginning of the Sub or Function...
0
by: Peter Vestergaard | last post by:
Hi, I am running VS .Net 2005. I have an application in which one of the classes are having a member that is an instance of a class defined in a managed C++ dll. As soon as I try to create an...
16
by: RdS | last post by:
Hello Would someone tell me the differences between following statements? When would you use one over the other? dim objtest1 as car dim objtest2 as new car dim objtest3 as car = new car ...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
11
by: blangela | last post by:
I am teaching a C programming course to C++ programmers and want to come up with list of _KEY_ differences between the two languages. Below is a list I have come up with so far. Have I missed any?...
4
by: florian.loitsch | last post by:
I wondered what should be the result of the following code: === function f() { x = false; function x() {}; alert(x); } === According to Ecmascript-spec we have the following rules: 10.1.3:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.