Prev | Current Page 186 | Next

Guy Fouch?©, Trey Nash

"Accelerated VB 2008"

SomeMethod
CHAPTER 6 n INTERFACES 106
Console.WriteLine("A.SomeMethod received " + x.ToString())
End Sub
End Class
Public Class EntryPoint
Shared Sub Main()
Dim ca As INonGeneric = New A()
ca.SomeMethod(123.456)
ca.SomeMethod("123 point 456")
End Sub
End Class
This example, when run, displays the following results:
A.SomeMethod received 123.456
A.SomeMethod received 123 point 456
In the previous example, the interface INonGeneric is not generic, but the method
SomeMethod(Of T) is, and unlike the earlier IGeneric.SomeMethod, it isn??™t limited to a specific
type for a specific implementing class. This example uses Option Strict On to limit the possible
implicit conversions and prove that disparate types are acceptable as arguments.
Contracts
You usually define a contract to facilitate communication between two types in your design.
For example, suppose you have a virtual zoo, and in your zoo, you have animals. Now, an
instance of your ZooKeeper needs a way to communicate to the collection of these ZooDweller
objects that they should fly to a specific location. However, not all animals can fly, so clearly,
not all of the types in the zoo can support this flying contract.
Implementing Contracts with Classes
Let??™s consider one way to manage the complexity of getting these creatures to fly from one
location to the next.


Pages:
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
Angry Birds zakłady bukmacherskie ciekawe ranking kasyn Romet