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. Compilerfehler CS0553

Compilerfehler CS0553

Scheduled Pinned Locked Moved C#
4 Posts 2 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.
  • D Offline
    D Offline
    daniel99
    wrote on last edited by
    #1

    How it is possible to convert from a base-class: public class Test : Object { .... } .... Object obj = new Object(); Test test = (Test)obj;:confused:

    P 1 Reply Last reply
    0
    • D daniel99

      How it is possible to convert from a base-class: public class Test : Object { .... } .... Object obj = new Object(); Test test = (Test)obj;:confused:

      P Offline
      P Offline
      pmarfleet
      wrote on last edited by
      #2

      You can't do this. Object is the base class and Test the subclass. The Object type knows nothing about the Test type, so you can't create a new instance of Object and cast to Test. Creating a new instance of Test and casting to Object is OK because Test derives from Object and therefore inherits its attributes and behaviour.

      Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

      D 1 Reply Last reply
      0
      • P pmarfleet

        You can't do this. Object is the base class and Test the subclass. The Object type knows nothing about the Test type, so you can't create a new instance of Object and cast to Test. Creating a new instance of Test and casting to Object is OK because Test derives from Object and therefore inherits its attributes and behaviour.

        Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

        D Offline
        D Offline
        daniel99
        wrote on last edited by
        #3

        Object obj = new Object(); Test test = new Test(); test = (Test)obj; Now I have an InvalidCastException System.InvalidCastException wurde nicht behandelt. Message="Das Objekt des Typs System.Object kann nicht in Typ MyApplication.Test umgewandelt werden." Source="MyApplication"

        P 1 Reply Last reply
        0
        • D daniel99

          Object obj = new Object(); Test test = new Test(); test = (Test)obj; Now I have an InvalidCastException System.InvalidCastException wurde nicht behandelt. Message="Das Objekt des Typs System.Object kann nicht in Typ MyApplication.Test umgewandelt werden." Source="MyApplication"

          P Offline
          P Offline
          pmarfleet
          wrote on last edited by
          #4

          daniel99 wrote:

          Object obj = new Object(); Test test = new Test(); test = (Test)obj; Now I have an InvalidCastException

          How is this different to the code in your first post? It still won't work, for the reasons I outlined earlier. I suggest you get a book on OOP and start reading.

          Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

          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