Chapter 3

Objects and Visual Basic

What's in this chapter?

Object-Oriented Terminology

Composition of an Object

Characteristics of Value Types versus Reference Types

Primitive Types

Commands: If Then, Else, Select Case

Common Value Types (Structures)

Common Reference Types (Classes)

XML literals

Parameter passing ByVal and ByRef

Variable scope

Working with Objects

Understanding Binding

Data type conversions

Creating Classes

Event Handling

Object-Oriented Programming

Wrox.com Code Downloads for this Chapter

The wrox.com code downloads for this chapter are found at www.wrox.com/remtitle.cgi?isbn=9781118314456 on the Download Code tab. The code is in the chapter 3 download and individually named according to the code filenames throughout the chapter.

This chapter takes you through the basic syntax of Visual Basic. With its transition many years ago to .NET, Visual Basic like all native .NET languages, became an object-oriented language. At the time this was a major transition, and even now you continue to talk about how Visual Basic supports the four major defining concepts required for a language to be fully object-oriented:

1. 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.
2. 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.
3. 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.
4. 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 subclass 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 4 discusses these four concepts in detail; this chapter focuses on the syntax that enables you to utilize classes that already implement these concepts. The concepts are then illustrated through a review of the core types that 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:

  • 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.
  • 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. You 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset