When to instantiate class object in do-loop

Posted on 2012-09-06
Last Modified: 2012-09-07
I use a class object named COMPANY.PART. This class has many properties and methods, among which is the ability to validate any given part number string for correct format, etc.

A typical use of this class would be;

dim oPart as new COMPANY.PART

if oPart.IsValid then
do something nice
do something mean
end if

My question is when to instantiate the class when processing a large list of potential part numbers. Should it be outside of the loop logic or inside the loop logic? i.e;

(outside of loop logic example)
[i]dim oPart as new COMPANY.PART


if opart.isvalid then
do something nice
do something mean

end while[/i]

- or -

(inside loop logic example)


dim oPart as new COMPANY.PART
if opart.isvalid then
do something nice
do something mean

end while[/i]

Does it make a difference performance wise which way it is done?


Question by:ordo
    LVL 20

    Expert Comment

    Outside would be better. As the no. of objects created/destroyed would be minimal.
    LVL 26

    Accepted Solution

    Hi ordo,

    It's just the same as instantiating any other class, if you are going to constantly initiate it, instantiate it as the class object outside the loop, then initiate/initialise it inside the loop.
    sub some sub () 
    Dim oPart as new COMPANY.PART = nothing
    for i = 1 to eternity
      oPart = something relevant
      if oPart isnot nothing then
        with oPart
          dim ret as string = .methodName(something cool)
          .propertyname = ret
          .dispose() ' cleanup, set it to nothing again
        end with
      end if
    end sub

    Open in new window

    You could do the same with a reader class looping through a string array of things to read.


    Author Closing Comment

    Thank you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now