locked
With...End With statement RRS feed

  • Question

  • This is an academic question: If I am simply changing a few properties of an object, does the With statement provide a performance advantage when the procedure executes?
    Friday, June 29, 2007 6:03 AM

Answers

  • If you make many references to members of an element that is qualified, such as MyForm.Controls.Item(Subscript), you can improve performance by using the With ... End With construction:

    Code Snippet

    With MyForm.Controls.Item(Subscript)   ' Evaluate the qualification once.
       .Name = "Control number " & CStr(Subscript)
       .Text = .Name
       ' Access other members of MyForm.Controls.Item(Subscript)
       .Refresh()
    End With



    The preceding code evaluates MyForm.Controls.Item(Subscript) only once. It can run more than twice as fast as requalifying every member access. However, if the element is not qualified, for example AnswerForm or Me, there is no performance improvement using With ... End With. For more information, see With...End With Statements.

    Source : Performance Optimization in Visual Basic .NET - Avoiding Requalification.
    Friday, June 29, 2007 6:15 AM
  •  

    No, he means things like Me, or ThisClass or ThisItem isn't qualified.

     

    Since it, to say it childish, only needs 1 dot in it, like

    Me.Name

    Me.Text

     

    That also counts for a TextBox, since you can use TextBox.Text immediatly.

     

    Say you have a class with some far going namespaces like TheControl.Effects.SpecialEffects.UselessEffects.Type = Bla

     

    In case you need to fill in more info in UselessEffects, you may use With ... With End statement to makes it 'faster'.

     

    Regards,

    Lennard

    Sunday, July 1, 2007 2:56 PM

All replies

  • Yes, if you are accessing an object more than once there is an advantage.
    Friday, June 29, 2007 6:06 AM
  • If you make many references to members of an element that is qualified, such as MyForm.Controls.Item(Subscript), you can improve performance by using the With ... End With construction:

    Code Snippet

    With MyForm.Controls.Item(Subscript)   ' Evaluate the qualification once.
       .Name = "Control number " & CStr(Subscript)
       .Text = .Name
       ' Access other members of MyForm.Controls.Item(Subscript)
       .Refresh()
    End With



    The preceding code evaluates MyForm.Controls.Item(Subscript) only once. It can run more than twice as fast as requalifying every member access. However, if the element is not qualified, for example AnswerForm or Me, there is no performance improvement using With ... End With. For more information, see With...End With Statements.

    Source : Performance Optimization in Visual Basic .NET - Avoiding Requalification.
    Friday, June 29, 2007 6:15 AM
  • I appologize if this is too elementary but please correct if I'm wrong it you don't mind.

     

    The following reference to a textbox object, me.txtFirstName, would be an example of an element that is not qualified and there would be no performance improvement using the with...end with construction when I programatically change several properties of this object.

     

    Is this the correct understanding of the info you forwarded to me?

     

    Really appreciate your time.

    Sunday, July 1, 2007 4:22 AM
  •  

    No, he means things like Me, or ThisClass or ThisItem isn't qualified.

     

    Since it, to say it childish, only needs 1 dot in it, like

    Me.Name

    Me.Text

     

    That also counts for a TextBox, since you can use TextBox.Text immediatly.

     

    Say you have a class with some far going namespaces like TheControl.Effects.SpecialEffects.UselessEffects.Type = Bla

     

    In case you need to fill in more info in UselessEffects, you may use With ... With End statement to makes it 'faster'.

     

    Regards,

    Lennard

    Sunday, July 1, 2007 2:56 PM