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

dim differences

RdS
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

Are not objtest2 and objtest3 doing the same thing? I understand the =
after variable declaration sets initial value but would like clarification.
:)

thanks.
Jul 28 '06 #1
16 1478
RdS,
| Would someone tell me the differences between following statements?
| dim objtest1 as car
Assuming Car is a reference type. Defines a variable that can hold a Car
object, however no Car object is created.

| dim objtest2 as new car
| dim objtest3 as car = new car
Defines variables that can hold Car objects, initializes those variables
with new Car objects.
| When would you use one over the other?
| dim objtest1 as car
| dim objtest2 as new car
I would use the first when the Car object was coming from someplace else
(such as the control variable in a For Each, or the return value from a
function).
| Are not objtest2 and objtest3 doing the same thing? I understand the =
| after variable declaration sets initial value but would like
clarification.
Yes they are the same thing,

| dim objtest3 as car = new car
I use it when I may create a derived class, but be certain that I'm only
using base class methods.

Dim s As Stream = New FileStream(...)

However generally the New FileStream is actually hidden in a factory method
someplace...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"RdS" <rd*@nospam.nospamwrote in message
news:eH**************@TK2MSFTNGP03.phx.gbl...
| 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
|
| Are not objtest2 and objtest3 doing the same thing? I understand the =
| after variable declaration sets initial value but would like
clarification.
| :)
|
| thanks.
|
|
Jul 28 '06 #2
RdS
Yes. It did.

One final question about
| dim objtest2 as new car
| dim objtest3 as car = new car
Defines variables that can hold Car objects, initializes those variables
with new Car objects.
Why not always use dim objtest2 as new car? The second requires more
typing, etc. Is there any reason why not always to use the first statement.
The reason I ask is because in some code by same author in same assembly I
find the use of both. Is this just preference or was their a reason?

Thanks again.
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.netwrote in
message news:u9**************@TK2MSFTNGP06.phx.gbl...
RdS,
| Would someone tell me the differences between following statements?
| dim objtest1 as car
Assuming Car is a reference type. Defines a variable that can hold a Car
object, however no Car object is created.

| dim objtest2 as new car
| dim objtest3 as car = new car
Defines variables that can hold Car objects, initializes those variables
with new Car objects.
| When would you use one over the other?
| dim objtest1 as car
| dim objtest2 as new car
I would use the first when the Car object was coming from someplace else
(such as the control variable in a For Each, or the return value from a
function).
| Are not objtest2 and objtest3 doing the same thing? I understand the =
| after variable declaration sets initial value but would like
clarification.
Yes they are the same thing,

| dim objtest3 as car = new car
I use it when I may create a derived class, but be certain that I'm only
using base class methods.

Dim s As Stream = New FileStream(...)

However generally the New FileStream is actually hidden in a factory
method
someplace...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"RdS" <rd*@nospam.nospamwrote in message
news:eH**************@TK2MSFTNGP03.phx.gbl...
| 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
|
| Are not objtest2 and objtest3 doing the same thing? I understand the =
| after variable declaration sets initial value but would like
clarification.
| :)
|
| thanks.
|
|


Jul 28 '06 #3
Hello RdS,

For the new question you mentioned

===================
| dim objtest2 as new car
| dim objtest3 as car = new car
Defines variables that can hold Car objects, initializes those variables
with new Car objects.
Why not always use dim objtest2 as new car? The second requires more
typing, etc.
===================

I think it is just a personal preference and there is no critical
requirement or best practice on this. For VB programmers, the first will
save more words. And there are some c# or other programmers which may
prefer the second one because they're accustomed to declare the variable
first and assign initial value through "=". :-)

Please feel free to post here if there is anything else you're wondering.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 28 '06 #4
RdS
Thanks again. :)
"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:fq**************@TK2MSFTNGXA01.phx.gbl...
Hello RdS,

For the new question you mentioned

===================
>| dim objtest2 as new car
| dim objtest3 as car = new car
Defines variables that can hold Car objects, initializes those variables
with new Car objects.

Why not always use dim objtest2 as new car? The second requires more
typing, etc.
===================

I think it is just a personal preference and there is no critical
requirement or best practice on this. For VB programmers, the first will
save more words. And there are some c# or other programmers which may
prefer the second one because they're accustomed to declare the variable
first and assign initial value through "=". :-)

Please feel free to post here if there is anything else you're wondering.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no
rights.

Jul 28 '06 #5
You're welcome:)

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 28 '06 #6
For the new question you mentioned
>
===================
>>dim objtest2 as new car
dim objtest3 as car = new car
Defines variables that can hold Car objects, initializes those
variables with new Car objects.

Why not always use dim objtest2 as new car? The second requires more
typing, etc.
===================
You would have to separate the declaration and assignment if you're doing
something like

dim x as someThing
for i as integer=0 to 9
x=new someThing(i)
x.doStuff()
next

Andrew
Jul 28 '06 #7
You could also check if the object is always used. For example if it is used
only in a particular branch of code, you may want to separate declaration
and instanciation to avoid creating the object even when it is not needed at
all...

--
Patrice

"RdS" <rd*@nospam.nospama écrit dans le message de news:
Oj**************@TK2MSFTNGP05.phx.gbl...
Yes. It did.

One final question about
>| dim objtest2 as new car
| dim objtest3 as car = new car
Defines variables that can hold Car objects, initializes those variables
with new Car objects.

Why not always use dim objtest2 as new car? The second requires more
typing, etc. Is there any reason why not always to use the first
statement. The reason I ask is because in some code by same author in same
assembly I find the use of both. Is this just preference or was their a
reason?

Thanks again.
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.netwrote in
message news:u9**************@TK2MSFTNGP06.phx.gbl...
>RdS,
| Would someone tell me the differences between following statements?
| dim objtest1 as car
Assuming Car is a reference type. Defines a variable that can hold a Car
object, however no Car object is created.

| dim objtest2 as new car
| dim objtest3 as car = new car
Defines variables that can hold Car objects, initializes those variables
with new Car objects.
| When would you use one over the other?
| dim objtest1 as car
| dim objtest2 as new car
I would use the first when the Car object was coming from someplace else
(such as the control variable in a For Each, or the return value from a
function).
| Are not objtest2 and objtest3 doing the same thing? I understand the =
| after variable declaration sets initial value but would like
clarification.
Yes they are the same thing,

| dim objtest3 as car = new car
I use it when I may create a derived class, but be certain that I'm only
using base class methods.

Dim s As Stream = New FileStream(...)

However generally the New FileStream is actually hidden in a factory
method
someplace...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"RdS" <rd*@nospam.nospamwrote in message
news:eH**************@TK2MSFTNGP03.phx.gbl...
| 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
|
| Are not objtest2 and objtest3 doing the same thing? I understand the =
| after variable declaration sets initial value but would like
clarification.
| :)
|
| thanks.
|
|



Jul 28 '06 #8
As I stated, I rarely do:

| | dim objtest3 as car = new car

I favor:

| | dim objtest2 as new car

Which you use doesn't matter IMHO, as long as you consistently use one.
I will use:

| | dim objtest3 as car = car.Factory()

Where Car.Factory is a method that creates (or possibly looks up a cached
car) for me.
| Is this just preference or was their a reason?

Having a mix of:

| | dim objtest2 as new car
| | dim objtest3 as car = new car

Feels like an odd-ball solution. At the very least it feels very
inconsistent.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"RdS" <rd*@nospam.nospamwrote in message
news:Oj**************@TK2MSFTNGP05.phx.gbl...
| Yes. It did.
|
| One final question about
| | dim objtest2 as new car
| | dim objtest3 as car = new car
| Defines variables that can hold Car objects, initializes those variables
| with new Car objects.
|
| Why not always use dim objtest2 as new car? The second requires more
| typing, etc. Is there any reason why not always to use the first
statement.
| The reason I ask is because in some code by same author in same assembly I
| find the use of both. Is this just preference or was their a reason?
|
| Thanks again.
|
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.netwrote in
| message news:u9**************@TK2MSFTNGP06.phx.gbl...
| RdS,
| | Would someone tell me the differences between following statements?
| | dim objtest1 as car
| Assuming Car is a reference type. Defines a variable that can hold a Car
| object, however no Car object is created.
| >
| | dim objtest2 as new car
| | dim objtest3 as car = new car
| Defines variables that can hold Car objects, initializes those variables
| with new Car objects.
| >
| >
| | When would you use one over the other?
| | dim objtest1 as car
| | dim objtest2 as new car
| I would use the first when the Car object was coming from someplace else
| (such as the control variable in a For Each, or the return value from a
| function).
| >
| >
| | Are not objtest2 and objtest3 doing the same thing? I understand the
=
| | after variable declaration sets initial value but would like
| clarification.
| Yes they are the same thing,
| >
| | dim objtest3 as car = new car
| I use it when I may create a derived class, but be certain that I'm only
| using base class methods.
| >
| Dim s As Stream = New FileStream(...)
| >
| However generally the New FileStream is actually hidden in a factory
| method
| someplace...
| >
| --
| Hope this helps
| Jay B. Harlow [MVP - Outlook]
| .NET Application Architect, Enthusiast, & Evangelist
| T.S. Bradley - http://www.tsbradley.net
| >
| >
| "RdS" <rd*@nospam.nospamwrote in message
| news:eH**************@TK2MSFTNGP03.phx.gbl...
| | 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
| |
| | Are not objtest2 and objtest3 doing the same thing? I understand the
=
| | after variable declaration sets initial value but would like
| clarification.
| | :)
| |
| | thanks.
| |
| |
| >
| >
|
|
Jul 28 '06 #9
RdS
Thanks to all for assistance.

"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.netwrote in
message news:eJ*************@TK2MSFTNGP06.phx.gbl...
As I stated, I rarely do:

| | dim objtest3 as car = new car

I favor:

| | dim objtest2 as new car

Which you use doesn't matter IMHO, as long as you consistently use one.
I will use:

| | dim objtest3 as car = car.Factory()

Where Car.Factory is a method that creates (or possibly looks up a cached
car) for me.
| Is this just preference or was their a reason?

Having a mix of:

| | dim objtest2 as new car
| | dim objtest3 as car = new car

Feels like an odd-ball solution. At the very least it feels very
inconsistent.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"RdS" <rd*@nospam.nospamwrote in message
news:Oj**************@TK2MSFTNGP05.phx.gbl...
| Yes. It did.
|
| One final question about
| | dim objtest2 as new car
| | dim objtest3 as car = new car
| Defines variables that can hold Car objects, initializes those
variables
| with new Car objects.
|
| Why not always use dim objtest2 as new car? The second requires more
| typing, etc. Is there any reason why not always to use the first
statement.
| The reason I ask is because in some code by same author in same assembly
I
| find the use of both. Is this just preference or was their a reason?
|
| Thanks again.
|
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.netwrote in
| message news:u9**************@TK2MSFTNGP06.phx.gbl...
| RdS,
| | Would someone tell me the differences between following statements?
| | dim objtest1 as car
| Assuming Car is a reference type. Defines a variable that can hold a
Car
| object, however no Car object is created.
| >
| | dim objtest2 as new car
| | dim objtest3 as car = new car
| Defines variables that can hold Car objects, initializes those
variables
| with new Car objects.
| >
| >
| | When would you use one over the other?
| | dim objtest1 as car
| | dim objtest2 as new car
| I would use the first when the Car object was coming from someplace
else
| (such as the control variable in a For Each, or the return value from
a
| function).
| >
| >
| | Are not objtest2 and objtest3 doing the same thing? I understand
the
=
| | after variable declaration sets initial value but would like
| clarification.
| Yes they are the same thing,
| >
| | dim objtest3 as car = new car
| I use it when I may create a derived class, but be certain that I'm
only
| using base class methods.
| >
| Dim s As Stream = New FileStream(...)
| >
| However generally the New FileStream is actually hidden in a factory
| method
| someplace...
| >
| --
| Hope this helps
| Jay B. Harlow [MVP - Outlook]
| .NET Application Architect, Enthusiast, & Evangelist
| T.S. Bradley - http://www.tsbradley.net
| >
| >
| "RdS" <rd*@nospam.nospamwrote in message
| news:eH**************@TK2MSFTNGP03.phx.gbl...
| | 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
| |
| | Are not objtest2 and objtest3 doing the same thing? I understand
the
=
| | after variable declaration sets initial value but would like
| clarification.
| | :)
| |
| | thanks.
| |
| |
| >
| >
|
|


Jul 28 '06 #10
RdS wrote:
Yes. It did.

One final question about
>| dim objtest2 as new car
| dim objtest3 as car = new car
Defines variables that can hold Car objects, initializes those variables
with new Car objects.

Why not always use dim objtest2 as new car? The second requires more
typing, etc. Is there any reason why not always to use the first statement.
The reason I ask is because in some code by same author in same assembly I
find the use of both. Is this just preference or was their a reason?
To answer this question we need a little history. In VB6, these code
snippets:

Dim car2 As New Car

vs

Dim car3 As Car
Set car3 = New Car

(note syntactic differences from VB.NET: you couldn't initialise in your
declarations, and assigning object references required the 'Set' keyword)

would actually have *significantly* different effects. The former syntax
often led to problems when used by the unwitting, and so was generally
avoided by the ... more witting.

So you may find the (VB.NET equivalent of the) latter code is found more
in the code of experienced VB6-ers who have migrated to VB.NET, and
haven't adopted the now-safe syntax, for whatever reason.

For my own part, I still do about half my work in VB6, so it is best for
me to avoid typing the former syntax even in VB.NET, lest my fingers
remember it too well and start typing it in VB6.

--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Jul 28 '06 #11

"Larry Lard" <la*******@googlemail.comwrote in message
news:4i************@individual.net...

<snip>

: To answer this question we need a little history. In VB6, these code
: snippets:
:
: Dim car2 As New Car
:
: vs
:
: Dim car3 As Car
: Set car3 = New Car
:
: (note syntactic differences from VB.NET: you couldn't initialise in your
: declarations, and assigning object references required the 'Set' keyword)
:
: would actually have *significantly* different effects. The former syntax
: often led to problems when used by the unwitting, and so was generally
: avoided by the ... more witting.

<snip>

I've seen this before, but I've never seen what the problem was. I always
use the latter (Dim car3 As Car: Set car3 = New Car) because I was told in
my formative years this to be the preferred approach but I was never told
*why* it's the preferred approach. Could you elaborate?

Thanx,

Ralf
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.
Jul 28 '06 #12
Larry (& a couple of others),
But! ;-)

The original poster didn't ask about:
| Dim car3 As Car
| Set car3 = New Car

Instead he asked about:

| Dim car3 As Car = New Car

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Larry Lard" <la*******@googlemail.comwrote in message
news:4i************@individual.net...
| RdS wrote:
| Yes. It did.
| >
| One final question about
| >| dim objtest2 as new car
| >| dim objtest3 as car = new car
| >Defines variables that can hold Car objects, initializes those
variables
| >with new Car objects.
| >
| Why not always use dim objtest2 as new car? The second requires more
| typing, etc. Is there any reason why not always to use the first
statement.
| The reason I ask is because in some code by same author in same assembly
I
| find the use of both. Is this just preference or was their a reason?
|
| To answer this question we need a little history. In VB6, these code
| snippets:
|
| Dim car2 As New Car
|
| vs
|
| Dim car3 As Car
| Set car3 = New Car
|
| (note syntactic differences from VB.NET: you couldn't initialise in your
| declarations, and assigning object references required the 'Set' keyword)
|
| would actually have *significantly* different effects. The former syntax
| often led to problems when used by the unwitting, and so was generally
| avoided by the ... more witting.
|
| So you may find the (VB.NET equivalent of the) latter code is found more
| in the code of experienced VB6-ers who have migrated to VB.NET, and
| haven't adopted the now-safe syntax, for whatever reason.
|
| For my own part, I still do about half my work in VB6, so it is best for
| me to avoid typing the former syntax even in VB.NET, lest my fingers
| remember it too well and start typing it in VB6.
|
| --
| Larry Lard
| la*******@googlemail.com
| The address is real, but unread - please reply to the group
| For VB and C# questions - tell us which version
Jul 29 '06 #13
Ralf,
| I've seen this before, but I've never seen what the problem was. I always
| use the latter (Dim car3 As Car: Set car3 = New Car) because I was told in
| my formative years this to be the preferred approach but I was never told
| *why* it's the preferred approach. Could you elaborate?

VB6's
| : Dim car2 As New Car

Would cause a car2 object to always exist if even you set car2 = Nothing.
This would/could hurt performance as the compiler would add code to most
usages of car2 to ensure that car2 always referred to an object. Also it was
hard to know when specifically the car2 object was created, as it is created
on the first "usage" of the car2 variable. Granted this also could simplify
some code as you get "for free" "lazy creation" of the car2 object.

VB.NET's
| : Dim car2 As New Car
Creates the car2 object on that specific line. If you set car2 = Nothing, a
new car2 object will not be created for you unless you explicitly

Hence in VB6 the "preferred" syntax was:
| Dim car3 As Car
| Set car3 = New Car

NOTE the two lines of code!
In .NET these two lines can be reduced to either:

Dim car2 As New Car
Dim car3 As Car = New Car

Which brings us back to the OP's questions, which line is "better". ;-) As
has been pointed out in this thread, its a matter of choice...
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"_AnonCoward" <ab****@uvwxyz.comwrote in message
news:YK******************@tornado.southeast.rr.com ...
|
| "Larry Lard" <la*******@googlemail.comwrote in message
| news:4i************@individual.net...
|
| <snip>
|
| : To answer this question we need a little history. In VB6, these code
| : snippets:
| :
| : Dim car2 As New Car
| :
| : vs
| :
| : Dim car3 As Car
| : Set car3 = New Car
| :
| : (note syntactic differences from VB.NET: you couldn't initialise in your
| : declarations, and assigning object references required the 'Set'
keyword)
| :
| : would actually have *significantly* different effects. The former syntax
| : often led to problems when used by the unwitting, and so was generally
| : avoided by the ... more witting.
|
| <snip>
|
| I've seen this before, but I've never seen what the problem was. I always
| use the latter (Dim car3 As Car: Set car3 = New Car) because I was told in
| my formative years this to be the preferred approach but I was never told
| *why* it's the preferred approach. Could you elaborate?
|
| Thanx,
|
| Ralf
| --
| ----------------------------------------------------------
| * ^~^ ^~^ *
| * _ {~ ~} {~ ~} _ *
| * /_``>*< >*<''_\ *
| * (\--_)++) (++(_--/) *
| ----------------------------------------------------------
| There are no advanced students in Aikido - there are only
| competent beginners. There are no advanced techniques -
| only the correct application of basic principles.
|
|
Jul 29 '06 #14
Dim objtest1 as car -early bound type, but no instance
Dim objtest2 as new car -early bound type, initialized with instance
Dim objtest3 as car = new car -early bound type, but no instance,
imedialty after this, an instance is assigned to it.

The first one is used when you want to assign an instance to it.
example:
dim x as sqldatareader
x = command.executereader

The second one is used when you need a new instance immediatly
Example:
dim x as new stringbuilder

The third one does the same as the second one.
I don't know if there is a performance difference between the two, but
it looks like the second one ought to be faster (no assignment)
The second one is less typing, so it has my preference.
RdS wrote:
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

Are not objtest2 and objtest3 doing the same thing? I understand the =
after variable declaration sets initial value but would like clarification.
:)

thanks.

Jul 29 '06 #15
Theo,
| I don't know if there is a performance difference between the two, but
| it looks like the second one ought to be faster (no assignment)
| The second one is less typing, so it has my preference.
Did you read the other posts in this thread?

The 2nd & 3rd are identical! In other words they are semantically the same,
although syntactically slightly different.

The easy way to see if they are identical or not is to look at the IL (using
a tool such as ILDASM.EXE).

.locals init ([0] class Test.Car objtest1,
[1] class Test.Car objtest2,
[2] class Test.Car objtest3)

IL_0000: nop
//000019: Dim objtest1 As Car ' early bound type, but no instance
//000020: Dim objtest2 As New Car ' early bound type, initialized
with instance
IL_0001: newobj instance void Test.Car::.ctor()
IL_0006: stloc.1
//000021: Dim objtest3 As Car = New Car ' early bound type, but no
instance, imedialty after this, an instance is assigned to it.
IL_0007: newobj instance void Test.Car::.ctor()
IL_000c: stloc.2
//000022:
Notice that both objtest2 & objtest3 have *exactly* the same IL produced,
ergo there is no semantical difference between the two lines!

Which means that *both* are either "early bound type, initialized with
instance" or they are both "early bound type, but no instance, imedialty
after this, an instance is assigned to it."
The only performance hit would be the fraction of a millisecond the compiler
would take to parse the longer line.

FWIW: This is an excellent example of knowing how to use ILDASM is
beneficial, you can see exactly what a VB language construct is doing to
you, and hopefully make an informed decision on whether using it or not is
advisable!

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Theo Verweij" <tv******@xs4all.nlwrote in message
news:e8**************@TK2MSFTNGP03.phx.gbl...
| Dim objtest1 as car -early bound type, but no instance
| Dim objtest2 as new car -early bound type, initialized with instance
| Dim objtest3 as car = new car -early bound type, but no instance,
| imedialty after this, an instance is assigned to it.
|
| The first one is used when you want to assign an instance to it.
| example:
| dim x as sqldatareader
| x = command.executereader
|
| The second one is used when you need a new instance immediatly
| Example:
| dim x as new stringbuilder
|
| The third one does the same as the second one.
| I don't know if there is a performance difference between the two, but
| it looks like the second one ought to be faster (no assignment)
| The second one is less typing, so it has my preference.
|
|
| RdS wrote:
| 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
| >
| Are not objtest2 and objtest3 doing the same thing? I understand the =
| after variable declaration sets initial value but would like
clarification.
| :)
| >
| thanks.
| >
| >
Jul 30 '06 #16
Completely clear! - yes I did read the other posts.
The only thing I was missing (what you explained to me now) was a
possible performance impact by using one or the other.

So, the second one is less typing (faster programming) and less compile
time (may be noticeable in big programs).

Great example of compile time optimization.

Thanks for the explanation.

Jay B. Harlow [MVP - Outlook] wrote:
Theo,
| I don't know if there is a performance difference between the two, but
| it looks like the second one ought to be faster (no assignment)
| The second one is less typing, so it has my preference.
Did you read the other posts in this thread?

The 2nd & 3rd are identical! In other words they are semantically the same,
although syntactically slightly different.

The easy way to see if they are identical or not is to look at the IL (using
a tool such as ILDASM.EXE).

.locals init ([0] class Test.Car objtest1,
[1] class Test.Car objtest2,
[2] class Test.Car objtest3)

IL_0000: nop
//000019: Dim objtest1 As Car ' early bound type, but no instance
//000020: Dim objtest2 As New Car ' early bound type, initialized
with instance
IL_0001: newobj instance void Test.Car::.ctor()
IL_0006: stloc.1
//000021: Dim objtest3 As Car = New Car ' early bound type, but no
instance, imedialty after this, an instance is assigned to it.
IL_0007: newobj instance void Test.Car::.ctor()
IL_000c: stloc.2
//000022:
Notice that both objtest2 & objtest3 have *exactly* the same IL produced,
ergo there is no semantical difference between the two lines!

Which means that *both* are either "early bound type, initialized with
instance" or they are both "early bound type, but no instance, imedialty
after this, an instance is assigned to it."
The only performance hit would be the fraction of a millisecond the compiler
would take to parse the longer line.

FWIW: This is an excellent example of knowing how to use ILDASM is
beneficial, you can see exactly what a VB language construct is doing to
you, and hopefully make an informed decision on whether using it or not is
advisable!
Jul 31 '06 #17

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

Similar topics

0
by: Dan Gass | last post by:
The difflib.py module and the diff.py tools script in Python 2.4 alpha 3 now support generating side by side (with intra line differences) in HTML format. I have found this useful for performing...
6
by: Martin Meyer im Hagen | last post by:
Hello, I've got installed Win 2003 SBS Premium with the SQL Server 2000 on a server machine. It works almost fine, except the application which uses the SQL Server. The main part of the...
2
by: Daniel | last post by:
Hi, Are there any differences between C# and VB.Net besides syntax? Performance-wise, how do they compare? Thanks, Dan
14
by: Bern | last post by:
what are all the diferences between the two?
4
by: effendi | last post by:
How can I extract differences between two arrays using Javascript? I hv for example, apple, orange, pear and another array with apple, orange and I would like to get pear. Thanks.
2
by: Patrick | last post by:
Are the differences between a search engine, a subject directory and a meta search engine significant for an ebusiness web site owner? A meta search engine merely uses ordinary existing search...
13
by: Kieran | last post by:
I am designing a content management system and I want to make sure all pages entered into it's database by users are using valid HTML. I have designed the system to use HTML 4.01 Transitional...
3
by: Daniel | last post by:
Are the differences between MSXML and .Net XSL transformation documented online anywhere? Many of my XSL's work in MSXML but transform differently in ..Net XSL transformation.
4
by: MS | last post by:
Just a general question here re VBA. Can anyone explain the differences between "!" and "." when refering to a control? eg Me!TxtBox and Me.TxtBox. What is difference between "+" and "&"...
4
by: Matt F | last post by:
Hey all, I'm a hobbyist programmer who usually codes in C++ or Java. I don't claim to be experts at either, and I'm not familiar with different types of variables, inheritance systems, etc. I've...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.