Section 25.1. Introduction


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 Object (as we did in Chapter 24), it would be nice if we could detect type mismatches at compile timethis 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 featuresgenericswhich 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 http://go.microsoft.com/fwlink/?LinkId=62990 .]

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.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net