Dynamic Typecasting

VB6 supported variant types and it was common to see code that depended on late bound objects.

VB6 and Visual Basic .NET support two kinds of reference use, early bound and late bound. Early bound references are references where the type of the object is known or resolved programmatically. Late bound references are resolved at runtime. Code like the following example relies on late binding:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles TextBox1.TextChanged

  MessageBox.Show(sender.Text)
End Sub

Clearly, sender is an Object type and Object does not have Text member. Because the Handles TextBox1.TextChanged clause indicates that the event handler responds to a Changed ...

Get Visual Basic® .NET Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.