Thursday, January 3, 2013

DirectCast in VB allows for downcasting!

The last line of C# code here will cause an error:

Cat cat = new Cat();
Lynx lynx = new Lynx();
Cat attemptedUpcast = (Cat) lynx;
Lynx attemptedDowncast = (Lynx) cat;

Share photos on twitter with Twitpic

 
 

But none of the following lines of VB Script are problematic:

Partial Class VBform
   Inherits System.Web.UI.Page
   Dim snake As Snake
   Dim viper As Viper
   Dim attemptedUpcasting As Snake = CType(viper, Snake)
   Dim attemptedDowncasting As Viper = DirectCast(snake, Viper)
End Class

Share photos on twitter with Twitpic

No comments:

Post a Comment