Chapter 12. Creating Classes in VBA

The ability to create self-contained software objects was first conceived in about 1970 with the development of SIMULA 67 (SIMUlation LAnguage), an extension of the scientific ALGOL 60 computer language.

It took quite a while before the programming community realized the implications of the breakthrough that SIMULA represented. When they did, object-oriented programming (OOP) quickly became the new buzzword, relegating structured programming to the realm of the lesser informed code cutters.

With the release of languages like SmallTalk, C++, and later, Java, OOP earned its place in the software hall of fame as the new panacea to all our programming ills. When Visual Basic 4.0 was released in 1993, Basic developers were tantalized by a new toy: the class module.

Long snubbed by C++ developers who had been using class modules for years, Basic developers were finally able to hold their heads high with the new found ability to create fully self-contained and reusable objects.

In OOP parlance, an object is a unique instance of a data structure, called a class, that has both properties, which define its characteristics, and executable procedures called methods, which define its behavior in modifying those properties.

A class's properties are completely isolated from the outside world and can only be modified internally by its own methods. This doesn't mean that the programmer can't do anything to them, but that he or she can't do anything to them directly; he or she must use those methods that are exposed for that purpose. The properties and methods you create are termed its implementation, whereas the methods it exposes to the programming environment constitutes its interface. Thus an object is a completely self-contained programmatic entity, in that it contains both its own data and the program code necessary to implement its own behavior.

In this chapter, we will examine VBA classes and class objects. We'll first look at what a class actually is and the difference between it and a class object. We'll then jump straight in and create our first class module and figure out how it works. After that, I will discuss how to identify classes and then how to get them to communicate with the rest of your application, before diving into the more advanced topics, like building collection classes, and then finishing off with some object-oriented theory to round it all off.

Classes are not as daunting as you might first think, and we hope that after reading this chapter you will cast off any fears you may have had and happily find many uses for your newfound skills.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.149.32