473,326 Members | 2,108 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,326 software developers and data experts.

Which declaration/assignment method do you prefer?

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
12 1196
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
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
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
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
Jeff,

Because you ask for opinions,

My answer is exactly the same as Mattias

Cor
Nov 20 '05 #6

"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
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
* "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
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
"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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
10
by: Christopher Benson-Manica | last post by:
Why can't I use a class destructor in a using declaration: using MyClass::~MyClass; ? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org ...
14
by: Blue Ocean | last post by:
My c++ text tells me that I should define methods this way: class Stack { int method(double t); Stack(int s); ... } int Stack::method(double t)
106
by: cfmortgagepro | last post by:
Hi, I know that I'm an extreme newb by asking this overly beaten question, but I am leaning toward C#, becuase the perception is that it is better to learn than VB.Net. I guess it makes you...
16
by: John Salerno | last post by:
My initial feeling is that concatenation might take longer than substitution, but that it is also easier to read: def p(self, paragraph): self.source += '<p>' + paragraph + '</p>\n\n' vs. ...
18
by: sunny | last post by:
Hi Why does C allows declaration of variable inside switch block. ex: foll prg does not gives "undeclared "b" error msg. but also does not initialize b to 20 int a=1; switch(a) { int b=20;...
4
by: emailscotta | last post by:
Below are some over simplified examples of code the create a single instance of an object and I was hoping some one could give me the pros and cons of each approach. First declaration: In this...
16
by: Anil Gupte/iCinema.com | last post by:
Can someone please explan the difference between Dim temp as String() and Dim temp() as String in terms of syntax and practical usage? I thought I knew, but of late have become confused...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.