Section 10.4. Constraints


10.4. Constraints

In the class definition:

     Public Class WorkGroup(Of T) 

the T type parameter can be assigned to any type when an instance of the class is created. Generics also allow you to place some restrictions or constraints on the set of types used. For instance, if your custom generic class is only designed to work with Windows Forms controls, allowing types like Integer and String could mess things up. Adding an As clause to the type parameter limits the range of data types that can be assigned to T. For example, the class definition:

     Public Class WorkGroup(Of T As System.Windows.Forms.Control) 

limits T to the System.Windows.Forms.Control class or any class derived from it. The following statements work:

     Dim anyControls As WorkGroup(System.Windows.Forms.Control)     Dim buttonsOnly As WorkGroup(System.Windows.Forms.Button) 

but statements using non-Control data types do not:

     ' ----- This doesn't work.     Dim buttonsOnly As WorkGroup(Integer) 

since Integer is not derived from System.Windows.Forms.Control.

This use of a type constraint to limit the selection of types used for T also applies to interfaces. The statement:

     Public Class WorkGroup(Of T As Runtime.Serialization.ISerializable) 

restricts the type-specific use of the WorkGroup class to only those types that implement the ISerializable interface.

In addition to these two flavors of type constraints (class and interface), the As clause is also used to establish a new constraint. This type of constraint restricts the allowed replacement types to only those types that have a parameterless constructorthat is, a constructor that takes no arguments. To use a new constraint, add the As New clause to the type parameter.

     Public Class WorkGroup(Of T As New) 

This enables the class to create new instances of the type replaced by T within the class's source code.

     Public Class WorkGroup(Of T As New)        Public Function DoSomeWork(  ) As T           ' ----- Create a new instance of whatever 'T' is.           Dim result As New T           ...           Return result        End Function End Class 

Because the As New clause prepares the class to create an instance of the type defined by the type parameter, you cannot specify any type for the type parameter that includes the MustInherit keyword. Such classes cannot be instantiated, and it would be meaningless (and an error) to use them with the New keyword.

Each defined type parameter can include a different constraint, the same constraint used with other type parameters, or no constraint at all. In this statement, T1 has no constraints placed on it, but the other type parameters do.

     Public Class WorkGroup(Of T1, T2 As New, T3 As New, _        T4 As Runtime.Serialization.ISerializable) 




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

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