brendan.hill
Member
- Joined
- Jun 20, 2010
- Messages
- 15
- Programming Experience
- 10+
OK I know this isn't supposed to be possible, because you can't be sure the object is really of the derived class (can't cast a mammal as a cat because it might be a dog, etc).
The thing is, I have a legitimate need to achieve this somehowand see no problem in this circumstance. I'm writing a simplified interface for XmlDocument, and I want to add my own functions, but inherit XmlDocument so I retain all the functionality.
When I try this code:
It can't cast from the base type XmlElement (which Me.AppendChild returns) to the derived type SimpleXmlElement (which is the return type of the function).
How can I do this? There must be some way to cast "down" in a controlled way, so you can wrap & extend classes like this.
-Brendan
One answer is not to inherit at all, but to wrap and expose each member one by one, but this seems tedious.
The thing is, I have a legitimate need to achieve this somehowand see no problem in this circumstance. I'm writing a simplified interface for XmlDocument, and I want to add my own functions, but inherit XmlDocument so I retain all the functionality.
When I try this code:
VB.NET:
Public Class SimpleXmlDoc
Inherits System.Xml.XmlDocument
Public Function addChild(ByVal anything As Object) As SimpleXmlElement
Return Me.AppendChild(AnythingToElement(anything, Me))
End Function
.
.
Public Class SimpleXmlElement
Inherits System.Xml.XmlElement
.
.
It can't cast from the base type XmlElement (which Me.AppendChild returns) to the derived type SimpleXmlElement (which is the return type of the function).
How can I do this? There must be some way to cast "down" in a controlled way, so you can wrap & extend classes like this.
-Brendan
One answer is not to inherit at all, but to wrap and expose each member one by one, but this seems tedious.