|
-
Oct 21st, 2002, 07:34 PM
#1
Thread Starter
PowerPoster
Casting Exception Help
Ok...This is really starting to tick me off. I am getting a casting exception when i perform the following code. My book I'm reading says that I can downcast (Base ~~> Derived Class):
Code:
using System;
using CastingExample;
namespace CastingExample
{
public class ClassA
{
public virtual void MyClassName()
{
Console.WriteLine("My Name is Class A");
}
}
public class ClassB : ClassA
{
public override void MyClassName()
{
Console.WriteLine("My Name is Class B");
}
public string HelloWorld()
{
return "Hello World";
}
}
}
public class TestHarness
{
public static void Main()
{
CastingExample.ClassA myClassA = new CastingExample.ClassA();
myClassA.MyClassName();
CastingExample.ClassB myClassB = new CastingExample.ClassB();
myClassB.MyClassName();
CastingExample.ClassA myNewA = (ClassA) myClassB;
myNewA.MyClassName();
// Casting Exception Here
CastingExample.ClassB myB = (ClassB) myClassA;
}
}
-
Oct 21st, 2002, 08:31 PM
#2
Frenzied Member
i couldnt find anything to solve this
http://www.aspfree.com/authors/jeff_...P/twisted6.asp
read the section on casting.. maybe u see something i dont
-
Oct 21st, 2002, 08:32 PM
#3
Thread Starter
PowerPoster
Thanks, I'll check it out. It really drives my nuts when I dont understand something..
-
Oct 21st, 2002, 08:42 PM
#4
Thread Starter
PowerPoster
It seems I have to cast the object in the following format:
Code:
// This does not work..
//CastingExample.ClassB myB = (ClassB) myClassA;
// This Does!
CastingExample.ClassB myB = (ClassB) myNewA;
So, myNewA object references the ClassB object on the heap, and b/c I explicitly cast the object to the new reference, it still points to the object, but its type has changed to support the ClassA interface. Therfore, I can not explicity downcast an object to a derived class. This is what I gathered from the above article..
Last edited by Lethal; Oct 21st, 2002 at 08:54 PM.
-
Oct 22nd, 2002, 01:01 PM
#5
Hyperactive Member
The problem with that was causing the exception was you were trying to upcast.
So, ClassB is a ClassA. But ClassA is not a ClassB.
In the first example, you casted ClassB to ClassA which is OK. ClassB has all of ClassB methods, and all of ClassA methods.
Then, you created a new ClassA. When you tried to cast ClassA to ClassB you got an error.
In the second example, you casted ClassB to ClassA. Then you casted this same object reference, which is still pointing to the same original object of type ClassB, back to ClassB. That works.
Did that help? Or am I just restating what you already found out for yourself??
-scott
he he he
-
Oct 22nd, 2002, 01:21 PM
#6
Thread Starter
PowerPoster
Yeah, thats pretty much what I figured out. It seems you can't directly downcast (Base object to Derived). I guess what confused me was that I thought when you cast an object, it makes a new copy of the object, but of a different type. However, you are still working with the original copy, but the public interface you are allowed to access changes.
-
Oct 22nd, 2002, 05:40 PM
#7
PowerPoster
Thanks for figuring this out. It might save me some time later on when I try to do the same thing. I am not being sarcastic either.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|