Object Oriented Programming (OOP) in VB
The use of Object-Oriented Programming (OOP) simplifies the top layer of code in a software program. It is self-documenting and makes the code easy to read for someone other than the author. Creating class modules to hold the OOP code allows the code to be written once and then reused many times in the same program or in other programs.
With regard to automation control software, the use of class modules for often-used objects such as digital I/O lines, pneumatic cylinders, motorized axes and the like, makes the project much easier to manage. You might be writing a control software package for a system with 100 or more I/O lines but you only need to write the actual methods (Subs or Functions) one time and they are shared by all of the I/O objects that you instantiated from that class module. The same is true for the cylinders and the motorized axes.
Objects are instantiations of a class module which contains properties and methods. Properties are simply variables or other objects. Methods are subroutines or functions. To assign a value to a property, use the Property Let() routine for variables or the Property Set() for assigning objects. To read the value of a variable or object, use the Property Get() function.
Objects can contain references to other objects to simplify the coding process. For example, a Cylinder object may contain references to the pneumatic control valve I/O line object and the limit switch I/O line objects. This allows you to encapsulate the code for actuating a Cylinder. If, for example, you name the Cylinder object “Door”, you can simply write code such as Door.Extend in the top layer code. Within the Door actuator cylinder object, the code turns on the valve object and waits for the extended sensor object to become active. If you are operating within a cooperative multi-tasking environment, write a Sub to start the extension of the door actuator and call it Door.StartExtend. This routine quickly turns on the air valve and returns. Then, while the cylinder is moving, you can process many other routines in the multi-tasking loop. To check on the cylinder to see if it has finished its move, write a Function such as Extended. This function would check the extended sensor to see if it has been activated or not. If it has been activated, it will return True, if not, it will return False. You must remember to insert a watchdog timing code within the code calling the Extended Function so that it will eventually terminate if the sensor malfunctions or the cylinder gets jammed. Write similar routines for the retract function of the cylinder.
Writing a class module is a good way of completely encapsulating the code for operating an object. It protects private routines from external access, while allowing external access by using the Public keyword before the Sub name. Class modules are not visible on the screen. If you need to display some data within the class module, you can either create an object and assign it to a Form object, or you can simply use a Form. A Form object is a type of class module which has a visible property and may be displayed. Otherwise, it can be used in the same way that a class module is. You may assign properties and methods and make properties read-only, write only or both read/write. You can make methods which are either private to the form or callable from without.
If you use a central library folder to contain your class and/or form modules, updating or debugging the module in one project will automatically update all projects using them when they are recompiled.
Use OOP to simplify your application code and make it more manageable in the future.