Q re With 
Author Message
 Q re With

Hi

Is

With New MyObject
        .MyProperty = "foo"
end with

like

Dim l_oMyObject as MyObject

Set l_oMyObject = New MyObject
l_oMyObject.MyProperty = "foo"
Set l_oMyObject = Nothing

or

Dim l_oMyObject as New MyObject

l_oMyObject.MyProperty = "foo"
Set l_oMyObject = Nothing

or  something else

I was under the impression that it was more like the first than the second.
But now I'm beginning to wonder whether I've got it wrong again!!

Thanks

Simon



Tue, 26 Aug 2003 17:50:34 GMT  
 Q re With
Ugh!  Either way, it's ugly.  Are you trying to save a few keystrokes, or is
there some other reason you would want to do this?  Just declare the object
and restrict "With" for repeated access to the same object.  IMO.

Steven

Quote:

>Is

>With New MyObject
>        .MyProperty = "foo"
>end with

>like

>Dim l_oMyObject as MyObject

>Set l_oMyObject = New MyObject
>l_oMyObject.MyProperty = "foo"
>Set l_oMyObject = Nothing

>or

>Dim l_oMyObject as New MyObject

>l_oMyObject.MyProperty = "foo"
>Set l_oMyObject = Nothing

>or  something else

>I was under the impression that it was more like the first than the second.
>But now I'm beginning to wonder whether I've got it wrong again!!



Wed, 27 Aug 2003 00:17:21 GMT  
 Q re With
Stephen

Thanks for your comments. I hope you don't mind me coming back to you.

Firstly, why do say it's ugly? I know there's always a subjective element to
what elegant code is but I'm wondering if there are reasons other than
purely aesthetic?

Secondly, I know if won't make too much difference speed wise but if I dim a
local variable, then use the with, doesn't vb create another local variable
of the same type and therefore your duplicating what vb does anyway. (I may
well be talking from ignorance here - it's a habit I'm struggling to throw
off!!)

e.g.

Dim l_oMyObject as MyObject

Set l_oMyObject = New MyObject

With l_oMyObject              <=== Doesn't vb create another hidden var here
of type MyObject
    .MyProperty = "foo"
    ...
end with

So

With New MyObject
    .MyProperty = "foo"
    ...
end with

Effectively the second saves you at least 2 lines of code (big deal - I hear
you say) and perhaps the creation of another variable. Also there's teardown
of only 1 object -the hidden var. With the first example, there's teardown
of the hidden var on executing the End With and then again of l_oMyObject
when out of scope. (yes, I know this is probably marginal!). The overhead is
that you need to expose the Me property of  MyObject if you want to
reference the instance.

I'm guessing here though. I await correction.

Thanks

Simon



Quote:
> Ugh!  Either way, it's ugly.  Are you trying to save a few keystrokes, or
is
> there some other reason you would want to do this?  Just declare the
object
> and restrict "With" for repeated access to the same object.  IMO.

> Steven


> >Is

> >With New MyObject
> >        .MyProperty = "foo"
> >end with

> >like

> >Dim l_oMyObject as MyObject

> >Set l_oMyObject = New MyObject
> >l_oMyObject.MyProperty = "foo"
> >Set l_oMyObject = Nothing

> >or

> >Dim l_oMyObject as New MyObject

> >l_oMyObject.MyProperty = "foo"
> >Set l_oMyObject = Nothing

> >or  something else

> >I was under the impression that it was more like the first than the
second.
> >But now I'm beginning to wonder whether I've got it wrong again!!



Wed, 27 Aug 2003 01:27:39 GMT  
 Q re With
When I remember correctly, the With statement performs a single
QueryInterface (opposed to a QueryInterface call each time a
member of an object is touched).
So using the With statement could improve performance.

I think Steven did not like the 'Dim x As New' statement, as
an object variable declared like this checks whether the object
is created each time you touch a member. Also, setting the
object variable to nothing and then touching a member of
the object will not cause an error, the object is re-created
automatically.
This behaviour will also change in VB.NET (object variable
will not be automatically re-created).

So any performance advantage you gain by using the With
statement gets lost when using object variables declared
with 'Dim As New'.

Of course, I also do not like the 'With New' statement
(didn't know that this possible).

Alex


Quote:
> Stephen

> Thanks for your comments. I hope you don't mind me coming back to you.

> Firstly, why do say it's ugly? I know there's always a subjective element
to
> what elegant code is but I'm wondering if there are reasons other than
> purely aesthetic?

> Secondly, I know if won't make too much difference speed wise but if I dim
a
> local variable, then use the with, doesn't vb create another local
variable
> of the same type and therefore your duplicating what vb does anyway. (I
may
> well be talking from ignorance here - it's a habit I'm struggling to throw
> off!!)

> e.g.

> Dim l_oMyObject as MyObject

> Set l_oMyObject = New MyObject

> With l_oMyObject              <=== Doesn't vb create another hidden var
here
> of type MyObject
>     .MyProperty = "foo"
>     ...
> end with

> So

> With New MyObject
>     .MyProperty = "foo"
>     ...
> end with

> Effectively the second saves you at least 2 lines of code (big deal - I
hear
> you say) and perhaps the creation of another variable. Also there's
teardown
> of only 1 object -the hidden var. With the first example, there's teardown
> of the hidden var on executing the End With and then again of l_oMyObject
> when out of scope. (yes, I know this is probably marginal!). The overhead
is
> that you need to expose the Me property of  MyObject if you want to
> reference the instance.

> I'm guessing here though. I await correction.

> Thanks

> Simon



> > Ugh!  Either way, it's ugly.  Are you trying to save a few keystrokes,
or
> is
> > there some other reason you would want to do this?  Just declare the
> object
> > and restrict "With" for repeated access to the same object.  IMO.

> > Steven


> > >Is

> > >With New MyObject
> > >        .MyProperty = "foo"
> > >end with

> > >like

> > >Dim l_oMyObject as MyObject

> > >Set l_oMyObject = New MyObject
> > >l_oMyObject.MyProperty = "foo"
> > >Set l_oMyObject = Nothing

> > >or

> > >Dim l_oMyObject as New MyObject

> > >l_oMyObject.MyProperty = "foo"
> > >Set l_oMyObject = Nothing

> > >or  something else

> > >I was under the impression that it was more like the first than the
> second.
> > >But now I'm beginning to wonder whether I've got it wrong again!!



Wed, 27 Aug 2003 01:44:16 GMT  
 Q re With
Correct.  Dim x As New Y and With New Y both have all the baggage associated
with using the New keyword on the declaration line.

Moreover, the With New construct means that you create a temporary variable,
with no other reference.  So you then use it and do something with it.  And
then it goes out of scope and disappears.  I cannot imagine a design where
that makes sense--thus I asked for intent <grin>.  I suspect that any such
code could be nonintuitive to maintain--thus the "ugly" comment.

Using With does not create a new object, but rather a reference to an
existing one.  Allocating a new object may be relatively heavy.  Getting a
reference to an existing one is usually very light.  So I see no performance
gain worth having (i.e. that justifies the "ugly" part as explained above)
by combing the allocation of a variable and the subsequent reference
acquired by "With".

Finally, as Alexander also noted, declaration and allocation on the same
line is changing for DOTNET.  That makes such code suspect and should be
avoided for conversion ease.

Steven

Quote:

>When I remember correctly, the With statement performs a single
>QueryInterface (opposed to a QueryInterface call each time a
>member of an object is touched).
>So using the With statement could improve performance.

>I think Steven did not like the 'Dim x As New' statement, as
>an object variable declared like this checks whether the object
>is created each time you touch a member. Also, setting the
>object variable to nothing and then touching a member of
>the object will not cause an error, the object is re-created
>automatically.
>This behaviour will also change in VB.NET (object variable
>will not be automatically re-created).

>So any performance advantage you gain by using the With
>statement gets lost when using object variables declared
>with 'Dim As New'.

>Of course, I also do not like the 'With New' statement
>(didn't know that this possible).

>Alex



>> Stephen

>> Thanks for your comments. I hope you don't mind me coming back to you.

>> Firstly, why do say it's ugly? I know there's always a subjective element
>to
>> what elegant code is but I'm wondering if there are reasons other than
>> purely aesthetic?

>> Secondly, I know if won't make too much difference speed wise but if I
dim
>a
>> local variable, then use the with, doesn't vb create another local
>variable
>> of the same type and therefore your duplicating what vb does anyway. (I
>may
>> well be talking from ignorance here - it's a habit I'm struggling to
throw
>> off!!)



Wed, 27 Aug 2003 02:38:54 GMT  
 Q re With
Alexander

Thanks

Quote:

> Of course, I also do not like the 'With New' statement
> (didn't know that this possible).

You are now the third person to state that they don't like the 'With New'
syntax. Everyone who's said that they don't like it obviously has a great
deal more knowledge than I do on matters VB and COM but no-one's explained
to me yet why it's not good. What am I missing? Is there any chance you
could elaborate?

I haven't seen the technique used anywhere except for an example of Matt
Curland's but he was using it with forms rather than object variables.

Thanks

Simon

PS I only found out a couple of days ago that could could pass "New Object"
as a parameter as well e.g.

ProcessObject New MyObject

 instead of

Dim l_oMyObject as MyObject

Set l_oMyObject = New MyObject
ProcessObject l_oMyObject

Private Sub ProcessObject(byref p_oMyObject as MyObject)
 ...
End Sub



Wed, 27 Aug 2003 02:55:53 GMT  
 Q re With
Hi Steven,

Quote:
>Correct.  Dim x As New Y and With New Y both have all the baggage associated
>with using the New keyword on the declaration line.

Can you actually prove that With New Y gives the same overhead as Dim
As New does? In the With New situation, there's no variable that the
compiler has to track to see if it has been set to Nothing, so I
really don't see a reason to why it should behave as Dim As New.

Quote:
>Moreover, the With New construct means that you create a temporary variable,
>with no other reference.  So you then use it and do something with it.  And
>then it goes out of scope and disappears.  I cannot imagine a design where
>that makes sense--thus I asked for intent <grin>.  I suspect that any such
>code could be nonintuitive to maintain--thus the "ugly" comment.

I know I use it in a few places. Here's one piece of code taken from
the UpTime sample on my site

With New CUpTime
  .ComputerName = Command$()
  If .Capture Then
    MsgBox "System uptime: " & _
           .Days & " day(s) " & _
           .Hours & " hour(s) " & _
           .Minutes & " minute(s) " & _
           .Seconds & " second(s).", vbInformation
  Else
    MsgBox "Failed to retrieve system uptime", vbCritical
  End If
End With

I wouldn't call it either nonintuitive or ugly, but of course that's
just my opinion  :-)

Matt

====================================

http://www.msjogren.net/dotnet/

CodeHound - The Software Developer's Search Engine
http://www.codehound.com



Thu, 28 Aug 2003 07:36:04 GMT  
 Q re With

Quote:

>Hi Steven,

>>Correct.  Dim x As New Y and With New Y both have all the baggage
associated
>>with using the New keyword on the declaration line.

>Can you actually prove that With New Y gives the same overhead as Dim
>As New does? In the With New situation, there's no variable that the
>compiler has to track to see if it has been set to Nothing, so I
>really don't see a reason to why it should behave as Dim As New.

>>Moreover, the With New construct means that you create a temporary
variable,
>>with no other reference.  So you then use it and do something with it.
And
>>then it goes out of scope and disappears.  I cannot imagine a design where
>>that makes sense--thus I asked for intent <grin>.  I suspect that any such
>>code could be nonintuitive to maintain--thus the "ugly" comment.

Use "New", and you allocate an object.  That's most of the overhead right
there.  As for the variable, there is a temporary variable created for you
by VB.  "With" is just a shortcut for something like this:

  Dim oTemp As Y    ' Allocate temp var
  Set oTemp = oSomeOtherY    ' Set it to With object
    ' Do all those .SomeProperty calls ...
  ' End scope-- End With forces temp var out of scope and releases it

As to proving it, hmm, that'll be hard.  {*filter*} VB probably talks about
this--I don't remember where I first learned that VB creates temp variables
behind the scenes--there is no "magic". <grin>

As to whether it behaves exactly like Dim As New, the only way to know for
sure is to ask the VB development group or run some test with several
thousand iterations to see if there is a performance difference.  (And it
will take that many iterations to show the extra conditions checks on each
reference.)

What I was referring to, however, was the awful behavior of the current "Dim
As New", where you can refer to the variable after it goes out of scope and
it then gets allocated again.  In other words, it hides bugs.  Hmm, that's
something that could be tested fairly easy ...

Steven



Fri, 29 Aug 2003 23:45:26 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. A97 Wizard Qs

2. Newbie Qs: Optimizing Stylesheet with Fields

3. Qs on applying style

4. Qs on applying style (word2000)

5. connect to robohelp qs

6. VERBATUM EXAM Qs 91947

7. handling event from control qs

8. 2 fairly simple database Qs

9. Qs: VB4.0/OLE/ODBC

10. DBCombos and some general Qs

11. Qs about OO design

12. Two Qs...

 

 
Powered by phpBB® Forum Software