[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

When to instantiate class object in do-loop

Posted on 2012-09-06
3
Medium Priority
?
501 Views
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
oPart.Validate("some-part-number-string")

if oPart.IsValid then
do something nice
else
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

while reader.read

oPart.Validate("some-part-number-string")
if opart.isvalid then
do something nice
else
do something mean
endif

end while[/i]

- or -

(inside loop logic example)

[i]while oreader.read

dim oPart as new COMPANY.PART
oPart.Validate("some-part-number-string")
if opart.isvalid then
do something nice
else
do something mean
endif

end while[/i]

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

Thanks.

Pat
0
Comment
Question by:ordo
3 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 38373631
Outside would be better. As the no. of objects created/destroyed would be minimal.
0
 
LVL 26

Accepted Solution

by:
Alan Warren earned 2000 total points
ID: 38375928
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
    i+=1
  end if

next
end sub

Open in new window

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

Alan
0
 

Author Closing Comment

by:ordo
ID: 38376258
Thank you.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

873 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