Private
Sub RunDemoButton_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
RunDemoButton.Click
Me.ResultsRichTextBox.Clear()
' Declare variable named textBox1 of type TextBox.
' Call the TextBox class' New method; assign a reference
' to the resulting TextBox object to the textBox1 variable.
Dim textBox1 As
New TextBox
' Declare variable named obj of type Object.
Dim obj As
Object
' Assign a reference to the object referenced by the textBox1 variable to
the obj variable.
obj = textBox1
' At this point:
' textBox1 references a TextBox object as type TextBox
' obj references the same TextBox object as type Object.
Me.ResultsRichTextBox.AppendText("Attempting
to cast object as TextBox..." & vbCrLf)
' Declare variable named textBox2 of type TextBox.
Dim textBox2 As TextBox
' Try to cast the object referenced by the obj variable as a TextBox.
textBox2 =
TryCast(obj, TextBox)
If textBox2 IsNot
Nothing Then
Me.ResultsRichTextBox.AppendText("Object
was successfully cast as type TextBox." & vbCrLf & vbCrLf)
Else
Me.ResultsRichTextBox.AppendText("Object
could not be cast as type TextBox." & vbCrLf & vbCrLf)
End If
Me.ResultsRichTextBox.AppendText("Attempting
to cast object as Button..." & vbCrLf)
' Declare variable named button1 of type Button.
Dim button1 As Button
' Try to cast the object referenced by the obj variable as a Button.
button1 =
TryCast(obj, Button)
If button1 IsNot
Nothing Then
Me.ResultsRichTextBox.AppendText("Object
was cast as type Button.")
Else
Me.ResultsRichTextBox.AppendText("Object
could not be cast as type Button. However, no exception was thrown by the
attempt to cast it, as would have been the case if DirectCast would have
been used.")
End If
End
Sub