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 to inhering type from inherited type

Casting to inhering type from inherited type

Scheduled Pinned Locked Moved C#
helpquestion
17 Posts 6 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.
  • L livez

    Hey! I got a problem with a class of mine inheriting from xmldocument. When I have an actual xmldocument I can´t cast it to my class, I get System.InvalidCastException. Why is this? I have done the same with for instance another class inheriting from UriBuilder.

    L Offline
    L Offline
    leppie
    wrote on last edited by
    #2

    livez wrote:

    When I have an actual xmldocument I can´t cast it to my class, I get System.InvalidCastException. Why is this?

    Because the instance you are trying to cast is obviously not of your inherited type!

    xacc.ide - now with TabsToSpaces support
    IronScheme - 1.0 alpha 4a out now (29 May 2008)

    L 1 Reply Last reply
    0
    • L leppie

      livez wrote:

      When I have an actual xmldocument I can´t cast it to my class, I get System.InvalidCastException. Why is this?

      Because the instance you are trying to cast is obviously not of your inherited type!

      xacc.ide - now with TabsToSpaces support
      IronScheme - 1.0 alpha 4a out now (29 May 2008)

      L Offline
      L Offline
      livez
      wrote on last edited by
      #3

      This is what I tried:

      class test : XmlDocument
      {
      public test()
      {

          }
      
          public void testing ()
          {
              test t = new test();
              XmlDocument doc = new XmlDocument();
              t = (test)doc;
          }
      }
      
      J L R 3 Replies Last reply
      0
      • L livez

        This is what I tried:

        class test : XmlDocument
        {
        public test()
        {

            }
        
            public void testing ()
            {
                test t = new test();
                XmlDocument doc = new XmlDocument();
                t = (test)doc;
            }
        }
        
        J Offline
        J Offline
        Jan Scamba
        wrote on last edited by
        #4

        [Message Deleted]

        L S 2 Replies Last reply
        0
        • L livez

          This is what I tried:

          class test : XmlDocument
          {
          public test()
          {

              }
          
              public void testing ()
              {
                  test t = new test();
                  XmlDocument doc = new XmlDocument();
                  t = (test)doc;
              }
          }
          
          L Offline
          L Offline
          leppie
          wrote on last edited by
          #5

          livez wrote:

          XmlDocument doc = new XmlDocument(); t = (test)doc;

          XmlDocument doc = new test();
          test t = (test) doc;

          I am not saying any more.

          xacc.ide - now with TabsToSpaces support
          IronScheme - 1.0 alpha 4a out now (29 May 2008)

          L 1 Reply Last reply
          0
          • J Jan Scamba

            [Message Deleted]

            L Offline
            L Offline
            livez
            wrote on last edited by
            #6

            Didnt work :(

            J 1 Reply Last reply
            0
            • L livez

              Didnt work :(

              J Offline
              J Offline
              Jan Scamba
              wrote on last edited by
              #7

              try leppies solution bellow

              1 Reply Last reply
              0
              • L leppie

                livez wrote:

                XmlDocument doc = new XmlDocument(); t = (test)doc;

                XmlDocument doc = new test();
                test t = (test) doc;

                I am not saying any more.

                xacc.ide - now with TabsToSpaces support
                IronScheme - 1.0 alpha 4a out now (29 May 2008)

                L Offline
                L Offline
                livez
                wrote on last edited by
                #8

                So I can´t cast to a more advanced type, but casting to a less advanced is okay.

                L 1 Reply Last reply
                0
                • L livez

                  So I can´t cast to a more advanced type, but casting to a less advanced is okay.

                  L Offline
                  L Offline
                  leppie
                  wrote on last edited by
                  #9

                  I think you need to study inheritance and polymorphism a bit more. It takes a while to get to understand it, but once you do, it all seems natural.

                  xacc.ide - now with TabsToSpaces support
                  IronScheme - 1.0 alpha 4a out now (29 May 2008)

                  L 1 Reply Last reply
                  0
                  • L leppie

                    I think you need to study inheritance and polymorphism a bit more. It takes a while to get to understand it, but once you do, it all seems natural.

                    xacc.ide - now with TabsToSpaces support
                    IronScheme - 1.0 alpha 4a out now (29 May 2008)

                    L Offline
                    L Offline
                    livez
                    wrote on last edited by
                    #10

                    Thanks for your help, though u could work on your manners a bit.

                    L C 2 Replies Last reply
                    0
                    • L livez

                      Thanks for your help, though u could work on your manners a bit.

                      L Offline
                      L Offline
                      leppie
                      wrote on last edited by
                      #11

                      My manners are irrelevant. I do not like to spoon-feed people.

                      xacc.ide - now with TabsToSpaces support
                      IronScheme - 1.0 alpha 4a out now (29 May 2008)

                      C 1 Reply Last reply
                      0
                      • L leppie

                        My manners are irrelevant. I do not like to spoon-feed people.

                        xacc.ide - now with TabsToSpaces support
                        IronScheme - 1.0 alpha 4a out now (29 May 2008)

                        C Offline
                        C Offline
                        Colin Angus Mackay
                        wrote on last edited by
                        #12

                        leppie wrote:

                        My manners are irrelevant.

                        Well said, Seven! :-D

                        Recent blog posts: * Introduction to LINQ to XML (Part 1) - (Part 2) - (part 3) My website | Blog

                        L 1 Reply Last reply
                        0
                        • L livez

                          Thanks for your help, though u could work on your manners a bit.

                          C Offline
                          C Offline
                          Colin Angus Mackay
                          wrote on last edited by
                          #13

                          livez wrote:

                          work on your manners a bit

                          His responses appeared to be quite polite. Curt, for sure, but polite.

                          Recent blog posts: * Introduction to LINQ to XML (Part 1) - (Part 2) - (part 3) My website | Blog

                          L 1 Reply Last reply
                          0
                          • J Jan Scamba

                            [Message Deleted]

                            S Offline
                            S Offline
                            Scott Dorman
                            wrote on last edited by
                            #14

                            Why was the message deleted? Because the OP said the proposed solution didn't work? That's not a very good reason as now no one else who runs into the same (or similar) problem will know not to try that approach.

                            Scott. —In just two days, tomorrow will be yesterday. —Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai


                            [Forum Guidelines] [Articles] [Blog]

                            1 Reply Last reply
                            0
                            • C Colin Angus Mackay

                              leppie wrote:

                              My manners are irrelevant.

                              Well said, Seven! :-D

                              Recent blog posts: * Introduction to LINQ to XML (Part 1) - (Part 2) - (part 3) My website | Blog

                              L Offline
                              L Offline
                              leppie
                              wrote on last edited by
                              #15

                              :)

                              xacc.ide - now with TabsToSpaces support
                              IronScheme - 1.0 alpha 4a out now (29 May 2008)

                              1 Reply Last reply
                              0
                              • C Colin Angus Mackay

                                livez wrote:

                                work on your manners a bit

                                His responses appeared to be quite polite. Curt, for sure, but polite.

                                Recent blog posts: * Introduction to LINQ to XML (Part 1) - (Part 2) - (part 3) My website | Blog

                                L Offline
                                L Offline
                                livez
                                wrote on last edited by
                                #16

                                Yes you´re quite right. My apologies Leppie.

                                1 Reply Last reply
                                0
                                • L livez

                                  This is what I tried:

                                  class test : XmlDocument
                                  {
                                  public test()
                                  {

                                      }
                                  
                                      public void testing ()
                                      {
                                          test t = new test();
                                          XmlDocument doc = new XmlDocument();
                                          t = (test)doc;
                                      }
                                  }
                                  
                                  R Offline
                                  R Offline
                                  Russell Jones
                                  wrote on last edited by
                                  #17

                                  in general these kinds of casts will only work towards the base class. ie a test is an XmlDocument but an XmlDocument is Not necessarily a test. you could use the "as" operator t = doc as test; this will return t == null if doc is not of type test. If you know how to convert an XmlDocument into a test then you could create a cast for it public static explicit operator Test(XmlDocument doc) { //your conversion algorithm here } HTH Russ

                                  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