Building classes and using them in your code is fairly simple, but there are a few points about Object-Oriented Programming (OOP) that can cause confusion. To help you make the most of Object-Oriented Programming and get up to speed, I’m including a list of related topics that are known to cause confusion to programmers— and not only beginners. If you understand the topics of the following paragraphs and how they relate to the topics discussed in the previous chapter “Building Custom Classes”, you’re more than familiar with the principles of OOP and you can apply them to your projects immediately.
Classes are templates that we use to create new objects. In effect, they’re the blueprints used to manufacture objects in our code. Another way to think of classes is as custom types. After you add the class Customer to your project (or a reference to the DLL that implements the Customer class), you can declare variables of the Customer type, just as you declare integers and strings. The code of the class is loaded into the memory, and a new set of local variables is created. This process is referred to as class instantiation: Creating an object of a custom type is the same as instantiating the class that implements the custom type. For each object of the Customer type, there’s a set of local variables, as they’re declared in the class’s code. The various procedures of the class are invoked as needed by the Common Language Runtime (CLR) and they act on the set of local variables that correspond to the current instance of the class. Some of the local variables may be common among all instances of a class: These are the variables that correspond to shared properties (properties that are being shared by all instances of a class).
When you create a new variable of the Customer type, the New() procedure of the Customer class is invoked. The New() procedure is known as the class’s constructor. Each class has a default constructor that accepts no arguments, even if the class doesn’t contain a New() subroutine. This default constructor is invoked every time a statement similar to the following is executed:
Dim cust As New Customer
You can overload the New() procedure by specifying arguments, and you should try to provide one or more parameterized constructors. Parameterized constructors allow you (or any developer using your class) to create meaningful instances of the class. Sure, you can create a new Customer object with no data in it, but a Customer object with a name and company makes more sense. The parameterized constructor initializes some of the most characteristic properties of the object.