Initialising object variables in VB.Net

Initialising object variables in VB.Net

Ask Question

11 years ago

11 years ago

992 times



In VB.Net is there any difference between the following three ways of initialising object variables

Method 1

Dim commandObject As SqlCommand
commandObject = New SqlCommand(“MadeUpCommand”)

Method 2

Dim commandObject As SqlCommand = New SqlCommand(“MadeUpCommand”)

Method 3

Dim commandObject As New SqlCommand(“MadeUpCommand”)

Is one more efficient than the others, or are they effectively all the same?

share|improve this question

asked Jan 20 ’09 at 10:48

Tim CTim C

66.9k1313 gold badges6363 silver badges8686 bronze badges

add a comment

3 Answers





There is no difference in the generated IL between the 3 methods.

share|improve this answer

answered Jan 20 ’09 at 10:55

Darin DimitrovDarin Dimitrov

899k236236 gold badges31023102 silver badges28082808 bronze badges

add a comment


Methods 1 and 2 are effectively the same. Method 1 obviously declares the object in a seperate statement to the assignment, but if the 2 lines are next to each other in code then it is also effectively the same as methods 2 and 3. In this case I would always use method 3 as it is the must succint. As darin says, they all generate the same IL.

I would only use method 1 when the declaration and assignment are required to have different scope, e.g. the assignment is done within an If block and the value needs to be tested outside that block.

share|improve this answer

answered Jan 20 ’09 at 11:00

Adam RalphAdam Ralph

25.9k44 gold badges5454 silver badges6464 bronze badges

add a comment


(2) and (3) are equivalent. I’d hope (1) gets optimised to be equivalent too (even if there are other local variables/instantiations)

Most coding standards I’ve seen suggest having the variable initialised at the point of declaration, and also having the point of declaration as close as possible to first use.

Of course, in VB.Net 2 and later, I’d prefer to see

Using commandObject As New SqlCommand(“MadeUpCommand”)
‘ Etc.
End Using

share|improve this answer

answered Jan 20 ’09 at 11:14

Rowland ShawRowland Shaw

35.2k1212 gold badges8787 silver badges154154 bronze badges

add a comment

Your Answer

Thanks for contributing an answer to Stack Overflow!Please be sure to answer the question. Provide details and share your research!But avoid …Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.To learn more, see our tips on writing great answers.

Draft saved
Draft discarded

Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Email and Password


Post as a guest


Required, but never shown

Post Your Answer


By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you’re looking for? Browse other questions tagged or ask your own question.


When laziness is efficient: Make the most of your command line

Ben Popper is the worst coder in the world: Something awry with my array

Featured on Meta

TLS 1.0 and TLS 1.1 removal for Stack Exchange services

Did Stack Exchange cut the number of negative comments nearly in half between…

An account of my meeting with the Stack Overflow management team


433Is there a conditional ternary operator in VB.NET?156Classes vs. Modules in VB.NET98Difference between DirectCast() and CType() in VB.NET14Differences in LINQ syntax between VB.Net and declaration and initialization2VB.Net: Initialising variables with the New constructor8Objects implicitly instantiated in data in VB.NET0Is slow?0How are VB.NET form variables handled

Hot Network Questions

How does the file command distinguish text and LaTeX files?

Can I get an honest review about my workout plan?

Resigning a couple of months before an event : is it abusive resignation?

If sound passes through material, vibration is produced. So are electromagnetic waves produced too?

Is there any dangerous command in LaTeX?

I like my new job but I’m always late and I skip work often. How to avoid this?

Feasible and rational way to give food and water to a prisoner?

Does Iran’s sanction play a pivotal role in rejecting papers from a journal?

Soggy Italian calzones

Player has trouble imagining the ingame situation

P = NP clarification

Converting JPG images to PNG

How do we convey scale in 2D games?

Reversed Iota’s

7zip : Why does encrypting the same file with AES-256 not give the same output?

Create a matrix from the rows of another, and calculate the rank of the new matrix

Using two tires on one wheel to prevent punctures?

Result object vs throwing exceptions

Why not save the queen by playing Qa5-a4?

How did Atari lose money on home computers?

Why do modern aircraft tend to have angular tails?

What happens if you can’t pay at an auction?

I have a 2nd broken spoke after 300km on a new bike, is it the wheel or me?

Is the conflict between Captain America and Tony Stark in “The Avengers” Blue Collar versus Educated Elite?

more hot questions

Question feed

Subscribe to RSS

Question feed
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.


Leave a Reply

Your email address will not be published. Required fields are marked *