Link to home
Start Free TrialLog in
Avatar of ordo
ordoFlag for United States of America

asked on

When to instantiate class object in do-loop

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
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Outside would be better. As the no. of objects created/destroyed would be minimal.
ASKER CERTIFIED SOLUTION
Avatar of Alan Warren
Alan Warren
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ordo

ASKER

Thank you.