General Scoping Question?

cliffd64

Member
Joined
Jun 7, 2005
Messages
13
Programming Experience
5-10
Hi all:

I have a general question concerning scoping of code within an application.

I have written an application that consists of a main (parent) form with a toolbar that shows several additional (child) forms based on the value selected on the toolbar.

I have noticed that each of the child forms share identical or nearly identical event handlers like the form load handler, the form close handler, and various common button click event handlers (for exporting to excel, or printing, or initiating a database query etc).

Is there a way to place/define this common code so that it is not replicated amongst each of the forms?

It would be great if these common elements could be defined once in the overall project and not have to be in each form.

Thanks in advance for any suggestions.
 
You could put it into a class module, then just instanciate it when you need it and call it. That's how I would do it.

Tg
 
You could create a form that includes this common code and then inherit each of your child forms from that base class. Inheritance is one of the gifts of OOP. This would be good practice for future use of inheritance too. TechGnome's suggestion will certainly work without issue, but one of the general rules of programming is keep scope as narrow as possible. Using a module means that your code is universally visible (at least within the universe of your project) while inheritance means it's only visible where it's needed.
 
jmcilhinney said:
You could create a form that includes this common code and then inherit each of your child forms from that base class. Inheritance is one of the gifts of OOP. This would be good practice for future use of inheritance too. TechGnome's suggestion will certainly work without issue, but one of the general rules of programming is keep scope as narrow as possible. Using a module means that your code is universally visible (at least within the universe of your project) while inheritance means it's only visible where it's needed.

This sounds like the approach I would need. My basic goal was to consolidate duplicate code/objects into a single reference where possible. THANKS!
 
Back
Top