A class is a program that doesn’t run on its own; it’s a collection of methods that must be used by another application. We exploit the functionality of the class by creating a variable of the same type as the class and then call the class’s properties and methods through this variable. The methods and properties of the class, as well as its events, constitute the class’s interface. It’s not a visible interface, like the ones you’ve learned to design so far, because the class doesn’t interact directly with the user. To interact with the class, the application uses the class’s interface, just as users will be interacting with your application through its visual interface.
You have already learned how to use classes. Now is the time to understand what goes on behind the scenes when you interact with a class and its members. Behind every object, there’s a class. When you declare an array, you’re invoking the System.Array class, which contains all the code for manipulating arrays. Even when you declare a simple integer variable, you’re invoking a class: the System.Integer class. This class contains the code that implements the various properties (such as MinValue and MaxValue) and methods (such as ToString) of the Integer data type. The first time you use an object in your code, you’re instantiating the class that implements this object. The class’s code is loaded into memory, initializes its variables, and is ready to execute. The image of the class in memory is said to be an instance of the class, and this is an object.
Objects are similar to Windows controls, except that they don’t have a visible interface. Controls are instantiated when you place them on a form; classes are instantiated when you use a variable of the same type — not when you declare the variable by using the Dim statement. To use a control, you must make it part of the project by adding its icon to the Toolbox, if it’s not already there. To use a class in your code, you must import the file that implements the class. (This is a Dynamic Link Library, or DLL, file.) To manipulate a control from within your code, you call its properties and methods. You do the same with classes. Finally, you program the various events raised by the controls to interact with the users of your applications. Most classes don’t expose any events because the user can’t interact with them, but some classes do raise events, which you can program just as you program the events of Windows controls.
Classes versus Objects
Two of the most misused terms in OOP are object and class, and most people use them interchangeably. You should think of the class as the factory that produces objects. There’s only one System.Array class, but you can declare any number of arrays in your code. Every array is an instance of the System.Array class. All arrays in an application are implemented by the same code, but they store different data. Each instance of a class is nothing more than a set of variables: the same code acts on different sets of variables; each set of variables is a separate instance of the class.
Consider three TextBox controls on the same form. They are all instances of the System.Windows. Forms.TextBox class, but changing any property of a TextBox control doesn’t affect the other two controls. Classes are the blueprints on which objects are based. We use the same blueprint to build multiple buildings with the same structural characteristics, but different properties (wall colors, doors, and so on).
Classes Combine Code with Data
Another way to view classes is to understand how they combine code and data. This simple idea is the very essence of object-oriented programming. Data is data, and traditional procedural languages allow you to manipulate data in any way. Meaningful data, however, is processed in specific ways.
Let’s consider accounting data. You can add or subtract amounts to an account, sum similar accounts (such as training and travel expenses), calculate taxes on certain account amounts, and the like. Other types of processing may not be valid for this type of data. We never multiply the amounts of two different accounts or calculate logarithms of account balances. These types of processing are quite meaningful with different data, but not with accounting data.
Because the data itself determines to a large extent the type of processing that will take place on the data, why not “package” the data along with the code for processing? Instead of simply creating structures for storing our data, we also write the code to process them. The data and the code are implemented in a single unit, a class, and the result is an object. After the class has been built, we no longer write code for processing the data; we simply create objects of this type and call their methods. To transfer an amount from one account to another, we call a method that knows how to transfer the amount, and it also makes sure that the amount isn’t subtracted from one account unless it has been added to the other account (and vice versa).
To better understand how classes combine code with data, let’s take a close look at a class we’re all too familiar with, the Array class. The role of the array is to store sets of data. In addition to holding data, the Array class also knows how to process data: how to retrieve an element, how to extract a segment of the array, and even how to sort its elements. All these operations require a substantial amount of code. The mechanics of storing data in the array and the code that implements the properties and the methods of the array are hidden from you, the developer. You can instruct the array to perform certain tasks by using simple statements. When you call the Sort method, you’re telling the array to execute some code that will sort its elements. As a developer, you don’t know how the data are stored in the array, or how the Sort method works. Classes abstract many operations by hiding the implementation details, and developers can manipulate arrays by calling methods. An instance of the Array class not only holds the elements that make up an array, but also exposes the most common operation one would perform on arrays as methods. Summing the logarithms of the elements of a numeric array is a specialized operation, and you have to provide the code to implement it on your own. If you type System.Array., you will see a list of all operations you can perform on an array.
In the following sections, you’ll learn how data and code coexist in a class, and how you can manipulate the data through the properties and methods exposed by the class. In Chapter “VB.NET – Programming Fundamentals” you learned how to create Structures to store data. Classes are similar to Structures, in that they represent custom data structures. In this chapter, we’ll take the idea of defining custom data structures one step further, by adding properties and methods for manipulating the custom data, something you can’t do with structures. Let’s start by building a custom class and then using it in our code