Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast parent class to child class

Tags:

c#

.net

casting

I have Message class which I have extended and added new property.

class ChildMessage: Message 
{
prop...
}

While trying to add Message Class to ChildMessage list I get Null reference for added class.

var myChildList =new List<ChildMessage> ();
var myParentClass = new Message();
myChildList.add(myParentClass as ChildMessage)

myChildList[0] == null //Always return null 

What is wrong with my code? What are the alternatives?

like image 441
Tomas Avatar asked Mar 07 '12 08:03

Tomas


People also ask

Can you cast a parent class to child in Java?

Therefore, there is an “is-a” relationship between the child and parent. Therefore, the child can be implicitly upcasted to the parent. However, a parent may or may not inherits the child's properties. However, we can forcefully cast a parent to a child which is known as downcasting.

Can a parent class be an instance of a child class?

Every child class has a parent class. Every instance of a child class has an instance of a parent class. A parent can be a child in another relationship.

Can you inherit child class parent class?

The child class inherits the attributes and functions of its parent class. If we have several similar classes, we can define the common functionalities of them in one class and define child classes of this parent class and implement specific functionalities there.


2 Answers

It is because a true instance of Message is not a ChildMessage, whereas an instance of ChildMessage is a Message.

Unless myParentClass is actually instantiated with ChildMessage you cannot use the cast operator to force a forward cast as the actual underlying type is Message.

That said, there are implicit and explicit cast operators available for use on types, so you could perhaps create one that takes a Message and outputs a ChildMessage.

http://msdn.microsoft.com/en-us/library/85w54y0a.aspx


Knowing that you are given these types and you have no choice but to use them, you have two options in order to extend that type.

First Option

Use the decorator/wrapper pattern to wrap a Message inside a ChildMessage:

class ChildMessage
{
    private Message _innerMessage;

    public ChildMessage(Message innerMessage)
    {
        _innerMessage = innerMessage;
    }
}

You then expose either the Message or its properties through the ChildMessage class.

Second Option

This is just as feasible, inherit from the Message, but when you want to make a ChildMessage from a Message you will need to convert it, not cast it:

class ChildMessage : Message
{
    public ChildMessage(Message m)
    {
        // Instantiate properties by copying from m
    }
}
like image 167
Adam Houldsworth Avatar answered Oct 14 '22 15:10

Adam Houldsworth


I think you somehow got it the wrong way - normaly you might want something like:

var myList =new List<Message> ();
var myChildObject = new ChildMessage();
myList.add(myChildObject)

myList[0] as ChildMessage == null // should not return null
like image 30
Random Dev Avatar answered Oct 14 '22 16:10

Random Dev