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********@yah oo.com> wrote in message
news:Of******** *****@TK2MSFTNG P11.phx.gbl...
I have the following classes:
Public Class BaseClass
{
..
}
Public Class DerivedClass: BaseClass
{
..
}
The following code will throw out an InvalidCastExce ption:
BaseClass base = new BaseClass();
DerivedClass derived = (DerivedClass)b ase;
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!