Prev | Current Page 59 | Next

Guy Fouch?©, Trey Nash

"Accelerated VB 2008"

x = 50
GraphCoord.y = 50
GraphCoord.PointDesc = "This is the graph point"
PrintCoordinates(Coord)
PrintCoordinates(GraphCoord)
PrintCoordinates(oCoord)
The first two calls to the PrintCoordinates function succeed because any object that
derives from the Coordinate class can be passed as a parameter. However, with Option
Strict On, the third call results in a compiler error, disallowing the implicit conversion of
oCoord to the Coordinate class. In order to pass oCoord as a parameter, you must ???cast it??? to
the Coordinate type, like this:
PrintCoordinates(CType(oCoord, Coordinate))
Another keyword related to reference type conversion is TypeOf. Use TypeOf before
using CType to determine if you can successfully convert the object. Prior to calling the
PrintCoordinates method, you could check the type of the oCoord variable and then call
the method:
CHAPTER 2 n VB 2008 SYNTAX 21
If TypeOf (oCoord) Is Coordinate Then
PrintCoordinates(CType(oCoord, Coordinate))
End If
DirectCast and TryCast
Two VB keywords related to casting reference types are DirectCast and TryCast. The DirectCast
function is used to cast reference types just like CType and has a similar syntax:
DirectCast(expression, Type)
However, DirectCast offers better performance because it directly attempts to convert the
specified type, and if it can??™t, it throws an exception.


Pages:
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
katalog stron dieta light Altanki wierszyki gry on line