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. .NET (Core and Framework)
  4. Converting from Base Class to Child Class

Converting from Base Class to Child Class

Scheduled Pinned Locked Moved .NET (Core and Framework)
algorithms
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.
  • B Offline
    B Offline
    bertvan
    wrote on last edited by
    #1

    Hello, I have a class, let's say, Document and a class which inherits from Document: DocumentLinked DocumentLinked adds an extra property, linkedId Now I'm searching through a list of documents, and I want to select some of them, and create DocumentLinked-objects out of them. But conversion fails. I tried Ctype overloading, which, according to the compiler, I cannot do because I would convert from a base-class. I quite bummed here. Some code:Public Class DocumentLinked Inherits Document Private _linkedId As Integer Public Property LinkedId() As Integer Get Return _linkedId End Get Set(ByVal value As Integer) _linkedId = value End Set End Property End Class '... ' selectedDocument is instance of Document ' linkedDocument is List( Of DocumentLinked) doc.LinkedDocuments.Add(CType(selectedDocument, DocumentLinked))

    C 1 Reply Last reply
    0
    • B bertvan

      Hello, I have a class, let's say, Document and a class which inherits from Document: DocumentLinked DocumentLinked adds an extra property, linkedId Now I'm searching through a list of documents, and I want to select some of them, and create DocumentLinked-objects out of them. But conversion fails. I tried Ctype overloading, which, according to the compiler, I cannot do because I would convert from a base-class. I quite bummed here. Some code:Public Class DocumentLinked Inherits Document Private _linkedId As Integer Public Property LinkedId() As Integer Get Return _linkedId End Get Set(ByVal value As Integer) _linkedId = value End Set End Property End Class '... ' selectedDocument is instance of Document ' linkedDocument is List( Of DocumentLinked) doc.LinkedDocuments.Add(CType(selectedDocument, DocumentLinked))

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

      bertburtbort wrote:

      Now I'm searching through a list of documents, and I want to select some of them, and create DocumentLinked-objects out of them. But conversion fails.

      The objects are Documents, they are not DocumentLinked objects. You cannot convert cast a reference from a Document to a DocumentLinked unless the actual object being referenced is already a DocumentLinked object already. If you want to convert the document then you are going to have to create a new object of the correct type and copy the relevant stuff in. -- modified at 5:51 Thursday 22nd February, 2007


      Upcoming events: * Glasgow: Geek Dinner (5th March) * Edinburgh: Web Security Conference Day for Windows Developers (12th April) My: Website | Blog | Photos

      B 1 Reply Last reply
      0
      • C Colin Angus Mackay

        bertburtbort wrote:

        Now I'm searching through a list of documents, and I want to select some of them, and create DocumentLinked-objects out of them. But conversion fails.

        The objects are Documents, they are not DocumentLinked objects. You cannot convert cast a reference from a Document to a DocumentLinked unless the actual object being referenced is already a DocumentLinked object already. If you want to convert the document then you are going to have to create a new object of the correct type and copy the relevant stuff in. -- modified at 5:51 Thursday 22nd February, 2007


        Upcoming events: * Glasgow: Geek Dinner (5th March) * Edinburgh: Web Security Conference Day for Windows Developers (12th April) My: Website | Blog | Photos

        B Offline
        B Offline
        bertvan
        wrote on last edited by
        #3

        So there is no way to convert downward in the inheritance-chain? Now I think of it, it makes sence :)

        C 1 Reply Last reply
        0
        • B bertvan

          So there is no way to convert downward in the inheritance-chain? Now I think of it, it makes sence :)

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

          bertburtbort wrote:

          So there is no way to convert downward in the inheritance-chain?

          That depends on what you think is down. To me down means towards object because that's at the base of everything. Also, be careful with terms like convert and cast. You cannot cast to a derived class unless the object is at least as derived as the class you are casting to. You can convert to anything if you write the code for it.


          Upcoming events: * Glasgow: Geek Dinner (5th March) * Edinburgh: Web Security Conference Day for Windows Developers (12th April) My: Website | Blog | Photos

          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