Objects and Visual Basic

WHAT you WILL LEARN IN THis cHAPTER_

V Object Oriented Terminology

V Composition of an Object

V Characteristics of Value Types versus Reference Types

V Primitive Types

V Commands: If Then, Else, Select Case

V Common Value Types (Structures)

V Common Reference Types (Classes)

V XML literals

V Parameter passing ByVal and ByRef

V Variable scope

V Working with Objects

V Understanding Binding

V Data type conversions

V Creating Classes

V Event Handling

V Advanced Object Oriented Programming

V Using Lambdas

Visual Basic supports the four major defining concepts required for a language to be fully object-oriented:

V Abstraction — Abstraction is merely the ability of a language to create "black box" code, to take a concept and create an abstract representation of that concept within a program. A Customer object, for instance, is an abstract representation of a real-world customer. A DataTable object is an abstract representation of a set of data.

V Encapsulation — Encapsulation is the concept of a separation between interface and implementation. The idea is that you can create an interface (public methods, properties, fields, and events in a class), and, as long as that interface remains consistent, the application can interact with your objects. This remains true even when you entirely rewrite the code within a given method — thus, the interface is independent of the implementation. The publicly exposed interface becomes what is known as a contract. It is this contract that you will look to limit changes to for those who consume your objects. For example, the algorithm you use to compute pi might be proprietary. You can expose a simple API to the end user, but hide all the logic used by the algorithm by encapsulating it within your class. Later if you change that algorithm, as long as the consumers of your object get the same results from your public interface, they won't need to make changes to support your updates. Encapsulation enables you to hide the internal implementation details of a class.

V Polymorphism — Polymorphism is reflected in the ability to write one routine that can operate on objects from more than one class — treating different objects from different classes in exactly the same way. For instance, if both the Customer and the Vendor objects have a Name property and you can write a routine that calls the Name property regardless of whether you are using a Customer or Vendor object, then you have polymorphism.

Visual Basic supports polymorphism in two ways — through late binding (much like Smalltalk, a classic example of a true object-oriented language) and through the implementation of multiple interfaces. This flexibility is very powerful and is preserved within Visual Basic.

V Inheritance — Inheritance is the concept that a new class can be based on an existing class gaining the interface and behaviors of that base class. The child or sub-class of that base or parent class is said to inherit the existing behaviors and properties. The new class can also customize or override existing methods and properties, as well as extending the class with new methods and properties. When inheriting from an existing class, the developer is implementing a process known as subclassing.

Chapter 3 discusses these four concepts in detail; this chapter focuses on the syntax that enables you to utilize classes which already implement these concepts. The concepts are then illustrated through a review of the core types which make up Visual Basic, as well as through the creation of a custom class that leverages these core concepts.

Visual Basic is also a component-based language. Component-based design is often viewed as a successor to object-oriented design, so component-based languages have some other capabilities. These are closely related to the traditional concepts of object orientation:

V Multiple interfaces — Each class in Visual Basic defines a primary interface (also called the default or native interface) through its public methods, properties, and events. Classes can also implement other, secondary interfaces in addition to this primary interface. An object based on this class has multiple interfaces, and a client application can choose with which interface it will interact with the object.

V Assembly (component) level scoping — Not only can you define your classes and methods as Public (available to anyone), Protected (available through inheritance), and Private (available only locally), you can also define them as Friend — meaning they are available only within the current assembly or component. This is not a traditional object-oriented concept, but is very powerful when used with component-based applications.

This chapter explains how to create and use classes and objects in Visual Basic. We won't get too deeply into code, but it is important that you spend a little time familiarizing yourself with basic object-oriented terms and concepts.

Was this article helpful?

0 0

Post a comment