R – How to reuse classes in the VB6 application

activexcode-reusecomponentsreusabilityvb6

First of all, as I am using VB6, please confine your kind suggestions to techniques applied to VB6.

alt text

I have a set of controls related to each other as the above figure shows. It includes several treeviews, a split bar, a listview, a subform( a usercontrol), and etc.

When I click or change the treeview nodes in the left, the right controls will change their display accordingly, and vice versa.

The data behind the scene is maintained in an Access database.

This set of data management and display is used in several different applications. So I wrote several classes to implement the logic and include these classes modules again and again in my applications.

So I am actually REUSE my classes in a "copy and paste" mode. It works but it have problems. If I make a change in a class, I have to change it in several applications.

These days I am thinking about making them into the so-called ActiveX components. But I am not sure which kind of ActiveX components should I develop to reuse the whole architecture.

In a nutshell, I want to know how can I reuse it more gracefully than just "copy and paste". Below is some ideas or expectation of the new "graceful REUSE", but not confine to them.

(1) I hope it looks like an ActiveX control which has a property page so that I can set some properties of it during design time.

(2) For different applications, the subform in the right may display different information and has different controls and may need extra coding and designing.

(3) Also I may need to code some new behaviour for the treeview and listview for different applications. This requirement make a whole usercontrol for the whole form not suitable. because MSDN said “References to ActiveX controls,should never be returned to client applications。"

Best Answer

  • Create a ActiveX DLL (not control)
  • Define a Interface for the form in
    the DLL
  • Move all your logic into one or more class in the DLL and have the routines interact with the form through the interface
  • Implement the Interface in the form
  • One initialization of the app have the form register itself with the ActiveX DLL

This will effectively eliminate copy and paste between the different apps.

For example for my metal cutting application I have a Shape Form, a Shape Screen class, and a bunch of shape classes. Two of the methods of the shape class are DrawScreen which has a parameters of type ShapeScreen, and GetValues which also has a parameter of type ShapeScreen.

DrawScreen uses the method of ShapeScreen to setup the entry screen and Shape Screen setup the Form through the IShapeForm interface. The GetValues uses Shape Screen methods to get the entered shape values which in turns uses the IShapeForm to get the values from the form.

This setup proved useful when we had to develop different shape entry forms in response to customer requests. The new form just implemented the IShapeForm interface and the rest of the software was untouched.

Related Topic