When you create a variable of any type, you’re creating an instance of a class. The variable lets you access the functionality of the class through its properties and methods. Even the base data types are implemented as classes (the System.Integer class, System.Double, and so on). An integer value, such as 3, is an instance of the System.Integer class, and you can call the properties and methods of this class by using its instance. Expressions such as CDec(3).MinValue and #1/1/2000#.Today are odd but valid. The first expression returns the minimum value you can represent with the Decimal data type, whereas the second expression returns the current date. The DataTime data type exposes the Today property, which returns the current date. The expression #1/1/2000# is a value of the DataTime type, so you can find out the current date by calling its Today property. If you enter either one of the preceding expressions in your code, you’ll get a warning, but they will be executed.
Classes are used routinely in developing applications, and you should get into the habit of creating and using custom classes, even with simple projects. In team development, classes are a necessity, because they allow developers to share their work easily. If you’re working in a corporate environment, in which different programmers code different parts of an application, you can’t afford to repeat work that someone else has already done. You should be able to get their code and use it in your application as is. That’s easier said than done, because you can guess what will happen as soon as a small group of programmers start sharing code — they’ll end up with dozens of different versions for each function, and every time a developer upgrades a function, he or she will most likely break the applications that were working with the old version. Or each time they revise a function, they must update all the projects by using the old version of the function and test them. It just doesn’t work.
The major driving force behind object-oriented programming (OOP) is code reuse. Classes allow you to write code that can be reused in multiple projects. You already know that classes don’t expose their source code. In other words, you can use a class without having access to its code, and therefore you can’t affect any other projects that use the class. You also know that classes implement complicated operations and make these operations available to programmers through properties and methods. The Array class exposes a Sort method, which sorts its elements. This is not a simple operation, but fortunately you don’t have to know anything about sorting. Someone else has done it for you and made this functionality available to your applications. This is called encapsulation. Some functionality has been built into the class (or encapsulated into the class), and you can access it from within your applications by using a simple method call.
The Framework is made up of thousands of classes, which allow you to access all the functionality of the operating system. You don’t have to see the code, and you don’t have to know anything about sorting to sort your arrays, just as you don’t need to know anything about encryption to encrypt a string by using the System.Security.Cryptography class. In effect, you’re reusing code that Microsoft has already written. It is also possible to extend these classes by adding custom members, and even override existing members. When you extend a class, you create a new class based on an existing one. Projects using the original class will keep seeing the original class, and they will work fine. New projects that see the derived class will also work.