caibeier
asked on
Convert base class object to derived class objcet
Hi experts, I was trying to use CType to convert a base class object to a derived class objcet, but got an invalid cast exception.
Here is class definition:
-------------------------- ---------
Public Class Car
...
End Class
Public Class SportsCar
Inherits Car
...
End Class
-------------------------- --------
Here is program code:
-------------------------- --------
dim myCar as New Car("Ford", "Black")
Dim mySportsar As SportsCar
mySportsar = CType(myCar, SportsCar)
My purpose is to make mySportsar a Black Ford, and use the member functions defined in SportsCar. But I got the following error
System.InvalidCastExceptio n: Specified cast is not valid
Thanks for your help!
Here is class definition:
--------------------------
Public Class Car
...
End Class
Public Class SportsCar
Inherits Car
...
End Class
--------------------------
Here is program code:
--------------------------
dim myCar as New Car("Ford", "Black")
Dim mySportsar As SportsCar
mySportsar = CType(myCar, SportsCar)
My purpose is to make mySportsar a Black Ford, and use the member functions defined in SportsCar. But I got the following error
System.InvalidCastExceptio
Thanks for your help!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.