Before printing out the values,
you unbox the value and copy the value contained in the object on the heap back into another
Integer on the stack. Unboxing is the opposite action to boxing and is the process of converting
the reference type variable back to a value type variable on the stack.
Reference Type Operators
VB includes operators that are helpful specifically when working with reference types. Given
the fact that explicit conversions can fail and throw exceptions, times arise when you want to
test the type of a variable first instead of performing a cast and seeing whether or not it fails.
The Is operator in conjunction with the TypeOf keyword returns a Boolean that determines if
you can convert the given expression to the given type as either a reference conversion or a
boxing or unboxing operation. For example, consider the following code:
Public Class EntryPoint
Public Class BaseType
Public x As Integer
Public y As Integer
End Class
Public Class DerivedType
Inherits BaseType
Public DT1 As Long
Public Description As String
End Class
Shared Sub Main()
Dim DerivedObj As DerivedType = New DerivedType()
Dim BaseObj1 As BaseType = New BaseType()
Dim BaseObj2 = DerivedObj
CHAPTER 2 n VB 2008 SYNTAX 23
If TypeOf BaseObj2 Is DerivedType Then
Console.
Pages:
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74