Solved

Convert base class object to derived class objcet

Posted on 2006-11-24
5
295 Views
Last Modified: 2008-01-09
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.InvalidCastException: Specified cast is not valid

Thanks for your help!
0
Comment
Question by:caibeier
5 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 18010109
That's because you are doing it backwards...

You can cast a DERIVED class to a BASE class, but not the other way around.

A SportsCar "is a" Car...but a Car is NOT necessarily a SportsCar.
0
 
LVL 34

Assisted Solution

by:Sancler
Sancler earned 250 total points
ID: 18010859
And I wonder, anyway, why - given that SportsCar Inherits Car, and so inherits its methods, including New - you cannot just put

Dim mySportsar As New SportsCar("Ford", "Black")

Roger
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help parsing JSON in my VB.Net application 4 96
vb.net help 4 31
No Data for DropDown List 2 26
How do sunrise and sunset times change with altitude 14 31
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question