When To Instantiate

  • Thread starter Thread starter John
  • Start date Start date
J

John

Another Best Performances Practice Question:
Here's the scenario, as some of you who viewed and responded to my
earlier question already know:
I have a public class (csValidation) which contains different validation
functions. These functions will be called from within many different
forms when a textbox is filled in by the user. I have an event in the
form that checks the textbox and opens up the validation function in the
csValidation class (thanks for the code, gabriel). When is the best
time to instantiate the csValidation class? Should I do it publicly (or
privately) in the form (Ex: public csValidate csV = new csValidate();),
or should I do it privately in the actual event that calls the
validation funtion?

TIA,
John
 
John, from the way you ask this question, maybe it's better to not
instantiate anything. Just use static methods.

This only applies if your validation methods should not store anything
between calls to the different validation methods. If so, what I would
use is static methods. For example, instead of doing this:

MyClass my = new MyClass();
[...]
my.ValidateA(blah blah blah);

do this:

MyClass.ValidateA(blah blah blah);

When ValidateA() is defined as static.

As for performance, I don't think there is a big hit. There might be,
though, if you instantiate a great many classes over and over at the same
time...

Anyway, before you go this route, make sure you don't do anything that
would be better solved by an actual instantiated class vs a class with
static methods.
 
Back
Top