By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,524 Members | 1,330 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,524 IT Pros & Developers. It's quick & easy.

Which declaration/assignment method do you prefer?

P: n/a
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?
Nov 20 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
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 Johnson [MVP: VB]" <i.***@enough.spam> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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?

Nov 20 '05 #2

P: n/a
It Early Binding or Late Binding problem !

I prefer Early Binding !

Bismark

"Jeff Johnson [MVP: VB]" <i.***@enough.spam> a écrit dans le message de
news:%2****************@TK2MSFTNGP12.phx.gbl...
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?

Nov 20 '05 #3

P: n/a
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 20 '05 #4

P: n/a
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 Johnson [MVP: VB]" <i.***@enough.spam> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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?

Nov 20 '05 #5

P: n/a
Jeff,

Because you ask for opinions,

My answer is exactly the same as Mattias

Cor
Nov 20 '05 #6

P: n/a

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:OU**************@tk2msftngp13.phx.gbl...
Are there any functional differences between the two?


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


Yeah, that much I knew. Thanks.
Nov 20 '05 #7

P: n/a
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 [MVP: VB]" <i.***@enough.spam> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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?

Nov 20 '05 #8

P: n/a
* "Jeff Johnson [MVP: VB]" <i.***@enough.spam> scripsit:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #9

P: n/a
Um, no it isn't.

--
Jonathan Allen
"Bismark Prods" <xa************@urbanet.ch> wrote in message
news:ce**********@newshispeed.ch...
It Early Binding or Late Binding problem !

I prefer Early Binding !

Bismark

"Jeff Johnson [MVP: VB]" <i.***@enough.spam> a écrit dans le message de
news:%2****************@TK2MSFTNGP12.phx.gbl...
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?


Nov 20 '05 #10

P: n/a
"Jeff Johnson [MVP: VB]" <i.***@enough.spam> wrote in news:#eRSdlJeEHA.3664
@TK2MSFTNGP12.phx.gbl:
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.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 20 '05 #11

P: n/a

"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@140.99.99.130.. .
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.


It's my personal preference as well. I just wondered what the prevailing
opinion was, and it seems to be the shorted method.
Nov 21 '05 #12

P: n/a
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

"Jeff Johnson [MVP: VB]" <i.***@enough.spam> wrote in message
news:eg**************@TK2MSFTNGP12.phx.gbl...

"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@140.99.99.130.. .
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.


It's my personal preference as well. I just wondered what the prevailing
opinion was, and it seems to be the shorted method.

Nov 21 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.