Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7609847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:16:26+00:00 2026-05-31T01:16:26+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:16:28+00:00Added an answer on May 31, 2026 at 1:16 am

    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
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web service , i add some extra class which have message
Within my actor I have to create a class which sends a message to
I have an class which has a enum property and a boolean property, based
I have a class on which I am overloading new and delete (these fetch
I have created new class which handles button. It does nothing (just for test,
I'm trying to send an email message using the .NET MailMessage class which can
I have a public static method string SomeMethod(string message) inside a SomeClass class which
I have a Message model class (which inherits from ActiveRecord::Base). For a particular deployment,
I have a class which is used to show correct message. This class works
I have a Message class which parses text messages using lookup tables. I receive

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.