Results 1 to 7 of 7

Thread: Casting Exception Help

  1. #1

    Thread Starter
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496

    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;
    	}
    }

  2. #2

  3. #3

    Thread Starter
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496

    Angry

    Thanks, I'll check it out. It really drives my nuts when I dont understand something..

  4. #4

    Thread Starter
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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.

  5. #5
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327
    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

  6. #6

    Thread Starter
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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.

  7. #7
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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
  •  



Click Here to Expand Forum to Full Width