Which declaration/assignment method do you prefer?

  • Thread starter Thread starter Jeff Johnson [MVP: VB]
  • Start date Start date
J

Jeff Johnson [MVP: VB]

When declaring a variable and assigning a value to it in one line, which way
do you do it:

Dim x As New SomeClass(<parameters>)

or

Dim x As SomeClass = New SomeClass(<parameters>)


Are there any functional differences between the two?
 
It Early Binding or Late Binding problem !

I prefer Early Binding !

Bismark
 
I prefer the second. I find my self sometimes having to change my code when
using a Try Catch block. The first method makes making those changes
easier. There is no difference (unlike VB 6).

Dim x As SomeClass

Try
x = New SomeClass(<paramters>)
Catch

Finally
x.DoSomething
End Try

Greg
 
Jeff,
When declaring a variable and assigning a value to it in one line, which way
do you do it:

Dim x As New SomeClass(<parameters>)

or

Dim x As SomeClass = New SomeClass(<parameters>)

I prefer #1. Less typing is a good thing.

Are there any functional differences between the two?

No. It certainly doesn't have anything to do with early/late binding
as someone suggested.



Mattias
 
There's no difference. I think the shorter syntax is just one of those
little ease-of-use gifts that the VB people at MS like to give to their
constituents to differentiate VB.Net from C#.

Tom Dacon
Dacon Software Consulting
 
Jeff,

Because you ask for opinions,

My answer is exactly the same as Mattias

Cor
 
No. It certainly doesn't have anything to do with early/late binding
as someone suggested.

Yeah, that much I knew. Thanks.
 
Jeff,
I normally use the first, unless I "need" to use the second.

I "need" to use the second when I am initializing the field from a function
(a factory method that does the New) or I want to declare the variable with
a base class, while initializing it with a sub class.

Dim x As SomeClass = CreateSomeClass(<parameters>)

Dim stream As Stream = New FileStream(<parameters>)

I will use the second when I want to make sure the routine does not use any
subclass specific methods, which in general is not very often.

Hope this helps
Jay
 
* "Jeff Johnson said:
When declaring a variable and assigning a value to it in one line, which way
do you do it:

Dim x As New SomeClass(<parameters>)

or

Dim x As SomeClass = New SomeClass(<parameters>)


Are there any functional differences between the two?

I use the first, if I never assign anything else to 'x', and I use the
latter if the variable is reused later (another object is assigned).
It's just about personal preference, but for me, this convention makes
my code more readable.
 
Dim x As SomeClass = New SomeClass(<parameters>)

I prefer this way because it is more logical.

You're creating a variable X of SomeClass. Then you assign X a new instance
of someclass.
 
I prefer this way because it is more logical.

You're creating a variable X of SomeClass. Then you assign X a new instance
of someclass.

It's my personal preference as well. I just wondered what the prevailing
opinion was, and it seems to be the shorted method.
 
Jeff (& Lucas),
FWIW: I find the shorter version equally logical:

Dim x As New SomeClass(<parameters>)

You're creating an initialized variable X of SomeClass.

Its all a matter of perspective: Is the glass half full or is it really half
empty?

Hope this helps
Jay
 

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

Back
Top