Appendix L : Generics


The syntax for declaring a generic is as follows:

  [attribute_list] [Partial] [accessibility] [Shadows] [inheritance] _  Class name[(Of type_list)]     [Inherits parent_class]     [Implements interface]     statements End Class 

All of these parts of the declaration are the same as those used by a normal (non-generic) class. See Chapter 16 and Appendix K for information about non-generic classes.

The key to a generic class is the (Of type_list) clause. Here, type_list is a list of data types separated by commas that form the generic’s parameter types. Each type can be optionally followed by the keyword As and a list of constraints that the corresponding type must satisfy. The constraint list can contain any number of interfaces and, at most, one class. It can also contain the New keyword to indicate that the corresponding type must provide an empty constructor. If a constraint list contains more than one item, the list must be surrounded by braces.

The following code defines the generic MyGeneric class. It takes three type parameters. The first is named Type1 within the generic’s code and has no constraints. The second type, named Type2, must satisfy the IComparable interface. The third parameter, named Type3, must provide an empty constructor, must satisfy the IDisposable interface, and must inherit directly or indirectly from the Person class.

  Public Class MyGeneric(Of _  Type1, _  Type2 As IComparable, _  Type3 As {New, IDisposable, Person}) 




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

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