Introduction to Objects in Visual Basic
An object is a structure that contains data and methods that manipulate the data. Almost everything that you do in Visual Basic is associated with objects. If you are new to object-oriented programming, the following terms and concepts will help you get started.
Classes and Objects
The words "class" and "object" are used so much in object-oriented programming that it is easy to confuse the terms. Generally speaking, a class is an abstract representation of something, whereas an object is a usable example of the thing the class represents. The one exception to this rule is shared class members, which are usable in both instances of a class and object variables declared as the type of the class.
Fields, Properties, Methods, and Events
Classes consist of fields, properties, methods, and events. Fields and properties represent information that an object contains. Fields are like variables because they can be read or set directly. For example, if you have an object named "Car" you could store its color in a field named "Color."
Properties are retrieved and set like fields, but are implemented by property Get and property Set procedures, which provide more control on how values are set or returned. The layer of indirection between the value being stored and the procedures that use this value helps isolate your data and lets you validate values before they are assigned or retrieved.
Methods represent actions that an object can perform. For example, a "Car" object could have "StartEngine," "Drive," and "Stop" methods. You define methods by adding procedures, either Sub routines or functions, to your class.
Events are notifications an object receives from, or transmits to, other objects or applications. Events enable objects to perform actions whenever a specific occurrence occurs. An example of an event for the "Car" class would be a "Check_Engine" event. Because Microsoft Windows is an event-driven operating system, events can come from other objects, applications, or user input such as mouse clicks or key presses.
Encapsulation, Inheritance, and Polymorphism
Fields, properties, methods, and events are only one half of the object-oriented programming equation. True object-orient programming requires objects to support three qualities: encapsulation, inheritance, and polymorphism.
Encapsulation means that a group of related properties, methods, and other members are treated as a single unit or object. Objects can control how properties are changed and methods are executed. For example, an object can validate values before enabling property changes. Encapsulation also makes it easier to change your implementation at a latter date by letting you hide implementation details of your objects, a practice called data hiding.
Inheritance describes the ability to create new classes based on an existing class. The new class inherits all the properties and methods and events of the base class, and can be customized with additional properties and methods. For example, you can create a new class named "Truck" based on the "Car" class. The "Truck" class inherits the "Color" property from the "Car" class and can have additional properties such as "FourWheelDrive."
Polymorphism means that you can have multiple classes that can be used interchangeably, even though each class implements the same properties or methods in different ways. Polymorphism is important to object-oriented programming because it lets you use items with the same names, regardless of what type of object is in use at the moment. For example, given a base class of "Car," polymorphism enables the programmer to define different "StartEngine" methods for any number of derived classes. The "StartEngine" method of a derived class named "DieselCar" may be completely different from the method with the same name in the base class. Other procedures or methods can use the "StartEngine" method of the derived classes in the same way, no matter what type of "Car" object is being used at the time.
Overloading, Overriding, and Shadowing
Overloading, overriding, and shadowing are similar concepts that can be easy to confuse. Although all three techniques enable you to create members with the same name, there are some important differences.
Overloaded members are used to provide different versions of a property or method that have the same name, but that accept different number of parameters, or parameters with different data types.
Overridden properties and methods are used to replace an inherited property or method that is not appropriate in a derived class. Overridden members must accept the same data type and number of arguments. Derived classes inherit overridden members.
Shadowed members are used to locally replace a member that has broader scope. Any type can shadow any other type. For example, you can declare a property that shadows an inherited method with the same name. Shadowed members cannot be inherited.