Tuesday 16 April 2013

Object-Oriented Programming in Visual Basic

Object-Oriented Programming in Visual Basic




Objects are central to Visual Basic programming. Forms and controls are objects. Databases are objects. If you've used Visual Basic for a while, or if you've worked through the examples in this Help file, then you've already programmed with objects, but there's a lot more to objects than what you've seen so far.

In the following topics, you'll see how easy it is to create your own objects from the classes you define, and to use objects to simplify your coding and increase code reuse.

Almost everything 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 get the terms mixed up. 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 are made of fields, properties, methods, and events. Fields and properties represent information that an object contains. Fields are like variables in that 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 using 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 allows you to 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 allow objects to perform actions whenever a specific occurrence takes place. 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 allowing 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 essential to object-oriented programming because it allows you to use items with the same names, no matter 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 Start Engine methods for any number of derived classes. The Start Engine method of a derived class named Diesel Car may be completely different from the method with the same name in the base class. Other procedures or methods can use the Start Engine method of the derived classes in exactly 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 allow 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.


No comments:

Post a Comment