Reputation: 18097
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?
Upvotes: 5
Views: 13193
Reputation: 8591
A cast on an object cannot be used to change its type: it is not a conversion. It only serves to tell the type of an object: you explain that you want to use the object as an object of that type; and the object must already be of that type, or you'll get an error (at least in a decent language such as C#).
Casting objects is meaningful because, thanks to inheritance, objects may be of multiple types at once.
In your code, every ChildMessage
is a Message
, but the Message
s you create are not ChildMessage
s, so trying to use them as such fails.
By contrast, "casts" from, say, integers to reals, or vice versa, may actually be conversions. (Terminology in IT is a hopeless jungle.)
Upvotes: 2
Reputation: 64467
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
}
}
Upvotes: 20
Reputation: 52270
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
Upvotes: 3
Reputation: 216243
If you change your code in this way
var myChildList =new List<Message> ();
var myChildClass = new ChildMessage();
myChildList.add(myChildClass as Message)
Now you have a List where the objects are of type ChildMessage or other types derived from Message. You could check the correct type later when you use the objects stored in the list.
Upvotes: 2