Home All Groups Group Topic Archive Search About
Author
28 Jul 2006 12:57 AM
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.

Author
28 Jul 2006 2:09 AM
Jay B. Harlow [MVP - Outlook]
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


Show quoteHide quote
"RdS" <rds@nospam.nospam> wrote in message
news:eHaNMEesGHA.4748@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.
|
|
Author
28 Jul 2006 2:30 AM
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.


Show quoteHide quote
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in
message news:u9qinresGHA.4444@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" <rds@nospam.nospam> wrote in message
> news:eHaNMEesGHA.4748@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.
> |
> |
>
>
Author
28 Jul 2006 3:05 AM
Steven Cheng[MSFT]
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.
Author
28 Jul 2006 3:42 AM
RdS
Thanks again.  :)


Show quoteHide quote
"Steven Cheng[MSFT]" <stch***@online.microsoft.com> wrote in message
news:fqsq$KfsGHA.2004@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.
>
Author
28 Jul 2006 7:11 AM
Steven Cheng[MSFT]
You're welcome:)

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.
Author
28 Jul 2006 9:40 AM
Andrew Morton
> 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
Author
28 Jul 2006 11:14 AM
Patrice
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" <rds@nospam.nospam> a écrit dans le message de news:
Ojgz73esGHA.4***@TK2MSFTNGP05.phx.gbl...
Show quoteHide quote
> 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]" <Jay_Harlow_***@tsbradley.net> wrote in
> message news:u9qinresGHA.4444@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" <rds@nospam.nospam> wrote in message
>> news:eHaNMEesGHA.4748@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.
>> |
>> |
>>
>>
>
>
Author
28 Jul 2006 12:02 PM
Jay B. Harlow [MVP - Outlook]
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


Show quoteHide quote
"RdS" <rds@nospam.nospam> wrote in message
news:Ojgz73esGHA.4380@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]" <Jay_Harlow_***@tsbradley.net> wrote in
| message news:u9qinresGHA.4444@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" <rds@nospam.nospam> wrote in message
| > news:eHaNMEesGHA.4748@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.
| > |
| > |
| >
| >
|
|
Author
28 Jul 2006 1:11 PM
RdS
Thanks to all for assistance.

Show quoteHide quote
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in
message news:eJLUz2jsGHA.372@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" <rds@nospam.nospam> wrote in message
> news:Ojgz73esGHA.4380@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]" <Jay_Harlow_***@tsbradley.net> wrote in
> | message news:u9qinresGHA.4444@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" <rds@nospam.nospam> wrote in message
> | > news:eHaNMEesGHA.4748@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.
> | > |
> | > |
> | >
> | >
> |
> |
>
>
Author
28 Jul 2006 2:57 PM
Larry Lard
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
larryl***@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Author
28 Jul 2006 4:15 PM
_AnonCoward
"Larry Lard" <larryl***@googlemail.com> wrote in message
news:4iumu7F5eu1rU1@individual.net...

<snip>

Show quoteHide quote
: 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.
Author
29 Jul 2006 7:05 PM
Jay B. Harlow [MVP - Outlook]
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


Show quoteHide quote
"_AnonCoward" <abc***@uvwxyz.com> wrote in message
news:YKqyg.12470$4c7.9551@tornado.southeast.rr.com...
|
| "Larry Lard" <larryl***@googlemail.com> wrote in message
| news:4iumu7F5eu1rU1@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.
|
|
Author
29 Jul 2006 7:02 PM
Jay B. Harlow [MVP - Outlook]
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:

Show quoteHide quote
| 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


Show quoteHide quote
"Larry Lard" <larryl***@googlemail.com> wrote in message
news:4iumu7F5eu1rU1@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
| larryl***@googlemail.com
| The address is real, but unread - please reply to the group
| For VB and C# questions - tell us which version
Author
29 Jul 2006 8:27 PM
Theo Verweij
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:
Show quoteHide quote
> 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.
>
>
Author
30 Jul 2006 2:49 PM
Jay B. Harlow [MVP - Outlook]
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


Show quoteHide quote
"Theo Verweij" <tverw***@xs4all.nl> wrote in message
news:e8Am610sGHA.1512@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.
| >
| >
Author
31 Jul 2006 9:13 AM
Theo Verweij
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:
Show quoteHide quote
> 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!
>