dcsimg
Results 1 to 16 of 16

Thread: Using with object .SomeMethod

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2019
    Posts
    64

    Using with object .SomeMethod

    Hi All,

    Curland's Advanced VB book states that:

    with object
    .somehing
    .somethingelse
    .thelastthing

    end with

    is more efficient than

    object.something
    object.somethingelse
    object.thelastthing

    Is this really true?

    I compiled and looked at the dissembled code with windbg and it looks like using with generates code and the actual instructions in the with block look the same as without it.

    cheers

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,413

    Re: Using with object .SomeMethod

    Depending on how you look at it, its only efficiency is in saving a few keystrokes. It doesn't make the code any more efficient. Personally I'm not a huge fan of it as I find it a distraction and sometimes can make it harder to read code. i've also seen code where there were nested with statements... which is ... wrong, just wrong, in my opinion. In my 30-ish years of using VB, I don't think I have ever once uttered "Oh my, you know what would make things so much easier, is if I didn't have to type this object's name more than once." I think perhaps the only time I may have used it, was when I was dealing with an object that had some properties buried several levels deep. In the end we found ourselves instead simply inheriting that object and creating the method and properties at first-class level as a shortcut.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2019
    Posts
    64

    Re: Using with object .SomeMethod

    I think it makes code harder to read also. If it has zero performance benefit then its just not worth it.

    thanks for your reply.

  4. #4
    New Member
    Join Date
    Jul 2019
    Posts
    8

    Re: Using with object .SomeMethod

    I think that you are naming the great advantages of the "with" statement:

    1. It saves a lot of keystrokes.
    2. I think that the code is more clear and easier to read.
    3. It makes the compiled code quicker and more efficient (I suppose Matthew Curland knows more about this subject than we do...)

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,413

    Re: Using with object .SomeMethod

    Quote Originally Posted by LeoCV View Post
    I think that you are naming the great advantages of the "with" statement:

    1. It saves a lot of keystrokes.
    2. I think that the code is more clear and easier to read.
    3. It makes the compiled code quicker and more efficient (I suppose Matthew Curland knows more about this subject than we do...)
    1) meh...
    2) To some people it might. To others it doesn't. To me it doesn't.
    3) No it doesn't. If it did, it would be in other languages too (or there'd be some implementation of it in other languages) ... but this is something that i generally unique to VB. It doesn't create any code faster or make it compile more efficiently. It's strictly just something for developers, it has no impact on the resulting code. It still needs to reach the target object and its properties.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    New Member
    Join Date
    Jul 2019
    Posts
    8

    Re: Using with object .SomeMethod

    Quote Originally Posted by techgnome View Post
    1) meh...
    2) To some people it might. To others it doesn't. To me it doesn't.
    3) No it doesn't. If it did, it would be in other languages too (or there'd be some implementation of it in other languages) ... but this is something that i generally unique to VB. It doesn't create any code faster or make it compile more efficiently. It's strictly just something for developers, it has no impact on the resulting code. It still needs to reach the target object and its properties.

    -tg
    1, 2) OK. It's a matter of everyone's preferences.
    3) Sure? Let's do a quick test: I've created a new project with a new form containing one textbox named Text1, and added the following code:
    Code:
    Option Explicit
    
    
    Private Sub Form_Load()
        Dim I As Long, Max As Long
        Dim D As Date
    
        Max = 100000000
    
        D = Now
        With Text1
            For I = 1 To Max
                .MaxLength = 100
            Next
        End With
        I = DateDiff("s", D, Now)
        MsgBox "Using With: " & CStr(I)
    
        D = Now
        For I = 1 To Max
            Text1.MaxLength = 100
        Next
        I = DateDiff("s", D, Now)
        MsgBox "Not using With: " & CStr(I)
        
    End Sub
    Results (within IDE):
    Using With: 8
    Not using With: 12

    Results (compiled code):
    Using With: 7
    Not using With: 11

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,413

    Re: Using with object .SomeMethod

    Compile it and look at the results there. Look at what compiles and actually runs.
    i'd also use something with more precision than DatEDillf in seconds... use a StopWatch and look at something finer than seconds. It wouldn't surprise me if the results were closer, but because the time you report are in seconds, it rounded off...
    Also, to make it a more fair asessment, put the With inside your loop.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    New Member
    Join Date
    Jul 2019
    Posts
    8

    Re: Using with object .SomeMethod

    You can also say that you thought the with statement didn't generate faster code and you've just discovered that it does...

  9. #9
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,345

    Re: Using with object .SomeMethod

    Quote Originally Posted by LeoCV View Post
    You can also say that you thought the with statement didn't generate faster code and you've just discovered that it does...
    The With Statement has no performance-increasing effect, when there was: "no dot in the expression"...
    (to give a simplified "rule of thumb").

    When there's at least one dot (also counting implicit ones) in the expression following the With Keyword,
    then there should be a performance-increasing effect (especially with OutOfProcess-Obj-Access like e.g. Excel-Automation)

    Your example shows a difference, but now try to put the following before the loops in your test:
    Code:
    Dim Text1 As VB.TextBox
    Set Text1 = Me.Text1
    When I do that here, the performance of your two loops is identical.

    HTH

    Olaf

  10. #10
    Frenzied Member
    Join Date
    Feb 2015
    Posts
    1,351

    Re: Using with object .SomeMethod

    When you use the 'With' statement you face with the some limitations with arrays:
    Code:
    Option Explicit
    
    Private Type tTest
        b As Byte
        s As String
    End Type
    
    Private Sub Form_Load()
        Dim b() As tTest
        Dim l As Long
        
        ReDim b(100)
    
        For l = 0 To 100
            
            With b(l)
                
                ReDim b(10)
    
            End With
            
        Next
    
    End Sub
    Besides, the 'With' statement has the bug:

    Code:
    Option Explicit
    
    Private Type tTest
        b As Byte
        s As String
    End Type
    
    Private Sub Form_Load()
        Dim b() As tTest
        Dim l As Long
        
        ReDim b(100)
        
        b(3).b = 1
        
        For l = 0 To 100
            
            With b(l)
            
                Select Case .b
                
                Case 0
                    .s = "Test"
                Case 1
                    Exit For
                End Select
    
            End With
            
        Next
        
        ReDim b(10)
        
    End Sub
    The array isn't unlocked when it go out of scope of the 'With'.

  11. #11

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Mar 2019
    Posts
    64

    Re: Using with object .SomeMethod

    So the definitive answer is that sometimes it helps?

    Edit To tricks point re array not being unlocked I seem to remember I also noticed that the reference count of the object gets incremented when you enter the with block but does not get decremented by end with but at some time later when everything goes out of scope.
    Last edited by vbwins; Aug 13th, 2019 at 02:13 AM.

  13. #13
    Member gilman's Avatar
    Join Date
    Jan 2017
    Posts
    58

    Re: Using with object .SomeMethod

    I try with this little project and the version with WITH seems to be a little faster than the version without
    PROJECT1.ZIP

  14. #14
    New Member
    Join Date
    Jul 2019
    Posts
    8

    Re: Using with object .SomeMethod

    Quote Originally Posted by Schmidt View Post
    The With Statement has no performance-increasing effect, when there was: "no dot in the expression"...
    (to give a simplified "rule of thumb").
    That's true, but even with no dots I get code simpler to read!

    Quote Originally Posted by Schmidt View Post
    Your example shows a difference, but now try to put the following before the loops in your test:
    Code:
    Dim Text1 As VB.TextBox
    Set Text1 = Me.Text1
    When I do that here, the performance of your two loops is identical.
    That's also true, but using With statemets I get the same performance increase with less code and the bonus of legibility.

    Of course (as I've previously stated) this advantages only make sense if you don't think that the code is harder to read...

  15. #15
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,491

    Re: Using with object .SomeMethod

    Quote Originally Posted by techgnome View Post
    3) No it doesn't. If it did, it would be in other languages too (or there'd be some implementation of it in other languages) ...
    Now this is hilarious as other languages will re-write consequtive common subexpression using With internally as part of the optimization phase and that's why the With keyword is not explicit, not the other way around :-))

    I mean *every* C/C++ optimizing compiler will internally re-write this

    some[5].access[idx].properties[10].one = 1;
    some[5].access[idx].properties[10].two = "test";
    some[5].access[idx].properties[10].three = new MyClass(42);


    . . . to something resembling With some[5].access[idx].properties[10] ... End With.

    VB's With is usually impl as a hidden local variable (array access is different) so comparing an explicit local var against hidden local var makes no difference in performance testing as demonstrated.

    Using With w/ deeply nested class-heirarchy makes a lot of sense though.

    cheers,
    </wqw>

  16. #16
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,426

    Re: Using with object .SomeMethod

    Yes, With... End With really shines when you deal with complex or deeply nested object models. Trying to work with an XML or HTML DOM or the MSChart object model without it is silly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width