473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

InvalidCastException

I have the following classes:

Public Class BaseClass
{
...
}

Public Class DerivedClass: BaseClass
{
...
}

The following code will throw out an InvalidCastException:

BaseClass base = new BaseClass();
DerivedClass derived = (DerivedClass)base;

What do I need to do to make the casting successfully?


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
4 1632
Here, you are trying to cast a generic type (base) to a specifc type (derived), which can't be done using a simple cast. Try using Type Convertors.
--
Rakesh Rajan
"Chris Li" wrote:
I have the following classes:

Public Class BaseClass
{
...
}

Public Class DerivedClass: BaseClass
{
...
}

The following code will throw out an InvalidCastException:

BaseClass base = new BaseClass();
DerivedClass derived = (DerivedClass)base;

What do I need to do to make the casting successfully?


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
Do you have that backward?

If you said
DerivedClass myderived = New DerivedClass();
BaseClass mybase = (BaseClass) myderived;

then I would understand you.

However, to do what you are trying to do does not make sense.

Say your derived class adds a method to the interface. Call it method "mm".

If you create a base object, and cast it to a derived type, then the
language should let you call the derived method "mm", right?

Now, let's say that the data needed by method "mm" was initialized in the
constructor of the derived class, WHICH YOU NEVER CALLED.

What should 'mm' do? If the language let's you do that, then I would not
use the language. My code could be forced into a situation that I could not
prepare for, except to totally abandon the concept of constructors, which is
wildly inefficient.

Thank goodness C# won't let you do this.

If you find a way, please tell me, so I can find a way to protect my code
from programmers who would use it!

--- Nick
"Chris Li" <ch********@yahoo.com> wrote in message
news:Of*************@TK2MSFTNGP11.phx.gbl...
I have the following classes:

Public Class BaseClass
{
..
}

Public Class DerivedClass: BaseClass
{
..
}

The following code will throw out an InvalidCastException:

BaseClass base = new BaseClass();
DerivedClass derived = (DerivedClass)base;

What do I need to do to make the casting successfully?


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3

Nick, you are providing a good point and I have to admit that I did not
provide enough information about this situation. In my case, the base
class and the derived class have the same signiture (property and
method). The derived does not add any new property or method. You might
question why I want to generate a derived class. Here is the answer:

I have a base class and 2 derive classes. They have the same signiture
but one of the properties is of Arraylist type. Depending on the type of
derived class, this Arraylist contain a different class.

But I want the ability to cast the base class to derived class. Maybe
the TypeConvert class provide the answer for me.


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
You want Generics, Chris. New feature of both C# 2.0 and Java 2.0... this
was already a feature of C++ that was overlooked in the release of C#.

Generics allow you to create classes that manage objects of a type where the
type is not known by the author of the generic class. The type is still
declared at design time, when the variable is declared, but one body of code
(one class) can be used to manage an arraylist where the arraylist manages a
list of elements of a type.

Google generics and c#

--- Nick

"Chris Li" <ch********@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...

Nick, you are providing a good point and I have to admit that I did not
provide enough information about this situation. In my case, the base
class and the derived class have the same signiture (property and
method). The derived does not add any new property or method. You might
question why I want to generate a derived class. Here is the answer:

I have a base class and 2 derive classes. They have the same signiture
but one of the properties is of Arraylist type. Depending on the type of
derived class, this Arraylist contain a different class.

But I want the ability to cast the base class to derived class. Maybe
the TypeConvert class provide the answer for me.


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Johan Appelgren | last post by:
Hi, I'm trying to serialize a class hierarchy using System.Xml.Serialization.XmlSerializer from a class that is loaded as a Com object. But it fails with a InvalidCastException. I don't know...
1
by: bob scola | last post by:
I have a csharp, VS 2003 solution for a winform application The application uses an object called a "matter" and the class is defined in matter.cs. I can load matter objects into a combobox ...
11
by: Roy Lawson | last post by:
I have no idea what is going on here. I wrote a simple application in VB.NET to generate a Crystal Report, and I am now trying to move it to ASP.NET with Crstal Enterprise. I wish I could tell...
4
by: DOTNET | last post by:
Hi, Anybody help me regarding this error: I am assigning the values to the session variables when the button is clicked and passing these session variables to the next page and when I am...
0
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is...
1
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it...
8
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
8
by: =?Utf-8?B?YXVsZGg=?= | last post by:
i trying to collect windows services. i'm getting inconsistencies. the collection works every time locally. but remote collection yields different result depending on my lab environment. 1) in...
4
by: rsdev | last post by:
Hi, I have an InvalidCastException which is completely puzzling me. I have checked all the members in the stored procedure against my data provider and seems to be ok. Also in the stack trace it...
8
by: Joe HM | last post by:
Hello - I was wondering that the "cleanest" way is to determine whether a CType() will throw an InvalidCastException? I have data I receive as an Object and I want to convert it to a String...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.