Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Casting object

Casting object

Scheduled Pinned Locked Moved C#
helpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matglas
    wrote on last edited by
    #1

    I was trying to "upper" cast a class. I looked like this but I get a Exception. Is there anyway to have a extended class and cast to it?

    public class FirstClass {
    
    }
    
    public class SecondClass : FirstClass {
    
    }
    
    public class Main {
    
      public Main() 
      {
        FirstClass iFirstC = new FirstClass();
        SecondClass iSecondC = (SecondClass)iFirstC;
      }
    }
    

    I get a error on the line.

    SecondClass iSecondC = (SecondClass)iFirstC;
    
    R C L 3 Replies Last reply
    0
    • M Matglas

      I was trying to "upper" cast a class. I looked like this but I get a Exception. Is there anyway to have a extended class and cast to it?

      public class FirstClass {
      
      }
      
      public class SecondClass : FirstClass {
      
      }
      
      public class Main {
      
        public Main() 
        {
          FirstClass iFirstC = new FirstClass();
          SecondClass iSecondC = (SecondClass)iFirstC;
        }
      }
      

      I get a error on the line.

      SecondClass iSecondC = (SecondClass)iFirstC;
      
      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      While syntactically correct, executing that statement will throw an exception, since an instance of iFirstC is not of type SecondClass. If you prefer not to handle exceptions, you should use the as operator and check for null.

      SecondClass iSecondC = iFirstC as SecondClass;
      if (iSecondC == null) {
      // Handle error
      }

      /ravi

      This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

      1 Reply Last reply
      0
      • M Matglas

        I was trying to "upper" cast a class. I looked like this but I get a Exception. Is there anyway to have a extended class and cast to it?

        public class FirstClass {
        
        }
        
        public class SecondClass : FirstClass {
        
        }
        
        public class Main {
        
          public Main() 
          {
            FirstClass iFirstC = new FirstClass();
            SecondClass iSecondC = (SecondClass)iFirstC;
          }
        }
        

        I get a error on the line.

        SecondClass iSecondC = (SecondClass)iFirstC;
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You can only cast SecondClass to FirstClass, but not FirstClass to SecondClass. FirstClass is parent, SecondClass is child.

        1 Reply Last reply
        0
        • M Matglas

          I was trying to "upper" cast a class. I looked like this but I get a Exception. Is there anyway to have a extended class and cast to it?

          public class FirstClass {
          
          }
          
          public class SecondClass : FirstClass {
          
          }
          
          public class Main {
          
            public Main() 
            {
              FirstClass iFirstC = new FirstClass();
              SecondClass iSecondC = (SecondClass)iFirstC;
            }
          }
          

          I get a error on the line.

          SecondClass iSecondC = (SecondClass)iFirstC;
          
          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          Matglas wrote:

          Is there anyway to have a extended class and cast to it?

          Only if the object is of the "extended class" (aka derived class). The following will work:

          FirstClass iFirstC = new SecondClass();
          SecondClass iSecondC = (SecondClass)iFirstC;

          This is because the actual object is a SecondClass even although it is initially referenced as a FirstClass. Does this help?


          Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

          M 1 Reply Last reply
          0
          • C Colin Angus Mackay

            Matglas wrote:

            Is there anyway to have a extended class and cast to it?

            Only if the object is of the "extended class" (aka derived class). The following will work:

            FirstClass iFirstC = new SecondClass();
            SecondClass iSecondC = (SecondClass)iFirstC;

            This is because the actual object is a SecondClass even although it is initially referenced as a FirstClass. Does this help?


            Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

            M Offline
            M Offline
            Matglas
            wrote on last edited by
            #5

            Thanks all for the great input. I start to understand it better now. So it does help.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups