Converting from Base Class to Child Class
-
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))
-
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))
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
Document
s, they are notDocumentLinked
objects. You cannot convert cast a reference from aDocument
to aDocumentLinked
unless the actual object being referenced is already aDocumentLinked
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
-
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
Document
s, they are notDocumentLinked
objects. You cannot convert cast a reference from aDocument
to aDocumentLinked
unless the actual object being referenced is already aDocumentLinked
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
-
So there is no way to convert downward in the inheritance-chain? Now I think of it, it makes sence :)
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