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. As Keyword, conversion of derived class to base class type

As Keyword, conversion of derived class to base class type

Scheduled Pinned Locked Moved C#
questiontutorial
6 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.
  • R Offline
    R Offline
    Raybarg
    wrote on last edited by
    #1

    Here is example code I made for this question:

    using System;
    namespace TestingAs
    {
    class baseClass
    {
    public string foo
    {
    get;
    set;
    }
    }
    class derivedClass : baseClass
    {
    public string bar
    {
    get;
    set;
    }
    }

    class Program
    {
        static void Main(string\[\] args)
        {
            derivedClass derived = new derivedClass();
            baseClass b = derived as baseClass;
            Console.WriteLine("type is {0}", b.GetType());
            Console.ReadLine();
        }
    }
    

    }

    I also tried just casting baseclass b = (baseClass)derived; which gives the same result: type is TestingAs.derivedClass Why b is not type of baseClass? How would I "convert" my derived class into base class type? Thank you in advance!

    A P OriginalGriffO 3 Replies Last reply
    0
    • R Raybarg

      Here is example code I made for this question:

      using System;
      namespace TestingAs
      {
      class baseClass
      {
      public string foo
      {
      get;
      set;
      }
      }
      class derivedClass : baseClass
      {
      public string bar
      {
      get;
      set;
      }
      }

      class Program
      {
          static void Main(string\[\] args)
          {
              derivedClass derived = new derivedClass();
              baseClass b = derived as baseClass;
              Console.WriteLine("type is {0}", b.GetType());
              Console.ReadLine();
          }
      }
      

      }

      I also tried just casting baseclass b = (baseClass)derived; which gives the same result: type is TestingAs.derivedClass Why b is not type of baseClass? How would I "convert" my derived class into base class type? Thank you in advance!

      A Offline
      A Offline
      Abhishek Sur
      wrote on last edited by
      #2

      This is the same concept of Reference Copying. In runtime the pointer of the object is set to derived class object. Thus it will always hold the derivedclassobject even if you cast it to base class object. :)

      Abhishek Sur


      My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

      **Don't forget to click "Good Answer" if you like to.

      1 Reply Last reply
      0
      • R Raybarg

        Here is example code I made for this question:

        using System;
        namespace TestingAs
        {
        class baseClass
        {
        public string foo
        {
        get;
        set;
        }
        }
        class derivedClass : baseClass
        {
        public string bar
        {
        get;
        set;
        }
        }

        class Program
        {
            static void Main(string\[\] args)
            {
                derivedClass derived = new derivedClass();
                baseClass b = derived as baseClass;
                Console.WriteLine("type is {0}", b.GetType());
                Console.ReadLine();
            }
        }
        

        }

        I also tried just casting baseclass b = (baseClass)derived; which gives the same result: type is TestingAs.derivedClass Why b is not type of baseClass? How would I "convert" my derived class into base class type? Thank you in advance!

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Convert.ChangeType ? Why would you want to?

        R 1 Reply Last reply
        0
        • R Raybarg

          Here is example code I made for this question:

          using System;
          namespace TestingAs
          {
          class baseClass
          {
          public string foo
          {
          get;
          set;
          }
          }
          class derivedClass : baseClass
          {
          public string bar
          {
          get;
          set;
          }
          }

          class Program
          {
              static void Main(string\[\] args)
              {
                  derivedClass derived = new derivedClass();
                  baseClass b = derived as baseClass;
                  Console.WriteLine("type is {0}", b.GetType());
                  Console.ReadLine();
              }
          }
          

          }

          I also tried just casting baseclass b = (baseClass)derived; which gives the same result: type is TestingAs.derivedClass Why b is not type of baseClass? How would I "convert" my derived class into base class type? Thank you in advance!

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Think about it. When you derive a class from baseClass, you are adding information to baseClass. When you try to convert your derived class to a baseClass, you would have to throw information away. You could not recover the information by re-casting your item to a derivedClass, because otherwise your program would have to behave very differently with the following :

          derivedClass d = new derivedClass();
          baseClass b = d as baseClass;
          derivedClass d2 = (derivedClass) b;

          and

          baseClass b = new baseClass();
          derivedClass d2 = (derivedClass) b;

          If the definitions were in (say) different methods to the casts, it could get very difficult to work out what is going on. Since wherever you can use a baseClass, you can use any derived class, C# is not keen on throwing away information, which could not be recovered by a re-cast back to the derived class.

          No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • P PIEBALDconsult

            Convert.ChangeType ? Why would you want to?

            R Offline
            R Offline
            Raybarg
            wrote on last edited by
            #5

            Does it matter why I want to? My app is using .dll's which are returning class arrays. My previous problem was that the application was completed and working well when it was required to use custom data in a way I found "good" solution to change my application to use derived class from the class type returned from the .dll layer, it just worked as long as I did not need to use same class array objects as parameters in .dll layer method calls.

            P 1 Reply Last reply
            0
            • R Raybarg

              Does it matter why I want to? My app is using .dll's which are returning class arrays. My previous problem was that the application was completed and working well when it was required to use custom data in a way I found "good" solution to change my application to use derived class from the class type returned from the .dll layer, it just worked as long as I did not need to use same class array objects as parameters in .dll layer method calls.

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              I didn't quite follow all that, but I still see no reason to downgrade the instances.

              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