Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Visual Basic 2005
How to Program, 3/e

ISBN:
0-13-186900-0
© 2005
pages: ~1500

Pre-order!
Amazon logo

This is the first in a series of three articles that shows how to declare and use generic methods in the Visual Basic programming language. In this article, we introduce the concept of generics. In Part 2 , we present an example using overloaded methods to motivate the need for generic methods. In Part 3 , we reimplement the overloaded methods from the example in Part 2 using a single generic method. These articles are intended for students who are already familiar with Visual Basic and for Visual Basic developers.
Download the code examples for this tutorial here.

[Note: This three-part tutorial is an excerpt (Sections 25.1-3) of Chapter 25, Generics, from our forthcoming textbook Visual Basic 2005 How to Program, 3/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., Visual Basic 2005 How to Program, ©2005. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

25.1 Introduction

In Chapter 24, we presented data structures that store and manipulate Object references. You could store any Object in these data structures. One inconvenient aspect of storing Object references occurs when retrieving them from a collection. An application normally needs to process specific types of objects. As a result, the Object references obtained from a collection typically need to be downcast to an appropriate type to allow the application to process the objects correctly. In addition, data of value types (e.g., Integer and Double) must be boxed to be manipulated with Object references, which increases the overhead of processing such data. Also, processing all data as type Object limits the Visual Basic compiler's ability to perform type checking.

Though we can easily create data structures that manipulate any type of data as Objects (as we did in Chapter 24), it would be nice if we could detect type mismatches at compile time—this is known as compile-time type safety. For example, if a Stack should store only Integer values, attempting to push a String onto that Stack should cause a compile-time error. Similarly, a Sort method should be able to compare elements that are all guaranteed to have the same type. If we create type-specific versions of class Stack and method Sort, the Visual Basic compiler would certainly be able to ensure compile-time type safety. However, this would require that we create many copies of the same basic code.

This chapter discusses one of Visual Basic's newest features—generics—which provides the means to create the general models mentioned above. Generic methods enable you to specify a set of related methods with a single method declaration. Generic classes enable you to specify a set of related classes with a single class declaration. Similarly, generic interfaces enable you to specify a set of related interfaces with a single interface declaration. Generics provide compile-time type safety. [Note: You can also implement generic Structures and Delegates. For more information, see the Visual Basic language specification version 8.0, available at www.microsoft.com/downloads/details.aspx? FamilyId=6D50D709-EAA4-
44D7-8AF3-E14280403E6E&displaylang=en
.]

We can write a generic method for sorting an array of objects, then invoke the generic method separately with an Integer array, a Double array, a String array and so on, to sort each different type of array. The compiler performs type checking to ensure that the array passed to the sorting method contains only elements of the same type. We can write a single generic Stack class that manipulates a stack of objects, then instantiate Stack objects for a stack of Integers, a stack of Doubles, a stack of Strings and so on. The compiler performs type checking to ensure that the Stack stores only elements of the same type.

This chapter presents examples of generic methods and generic classes. It also considers the relationships between generics and other Visual Basic features, such as overloading and inheritance. Chapter 26, Collections, discusses the .NET Framework's generic and non-generic collections classes. A collection is a data structure that maintains a group of related objects or values. The .NET Framework collection classes use generics to allow you to specify the exact types of object that a particular collection will store.

Tutorials in This Series:
Introduction to Generics in Visual Basic (You are here.)
Motivation for Generic Methods
Generic Method Implementation

Tutorial Index