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 8033397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:41:01+00:00 2026-06-05T01:41:01+00:00

I have the following classes: public class BaseContainer { public BaseItem item {get; set;}

  • 0

I have the following classes:

public class BaseContainer
{
   public BaseItem item {get; set;}
}

public class ExtendedContainer : BaseContainer
{
   new public ExtendedItem item {get; set;}
}

public class BaseItem{
   public string Name { get; set; }
}

public class ExtendedItem : BaseItem
{
   public string Surname { get; set; }
}

Then I have the following instructions:

ExtendedItem ei = new ExtendedItem { Name = "MyName", Surname = "MySurname" };
ExtendedContainer ec = new ExtendedContainer { item = ei };
BaseContainer bc = ec;
string temp = bc.item.Name;  // bc.item is null, why?

Why bc.item is null? It shouldn´t because ExtendedItem is a subclass if BaseItem, but when I run the code it is null. Can anyone help me with this issue?

The reason I´m doing this is that I´m using MVC3 and I´m passing to partial views objects/models that are part of a similar structure, and I normally just need the properties in the base class.

Thanks in advance.

UPDATE

Jon is right, I´m accessing different objects. What I did to fix it is to use constructors to set the item properties to the same object. Like this:

public class BaseContainer
{
    public BaseItem item {get; set;}
    public BaseContainer(BaseItem item)
    {
        this.item = item;
    }
}

public class ExtendedContainer : BaseContainer
{
    public ExtendedItem item {get; set;}

    public ExtendedContainer(ExtendedItem item) : base(item)
    {
        this.item = item;
    }
}

public class BaseItem{
    public string Name { get; set; }
}

public class ExtendedItem : BaseItem
{
    public string Surname { get; set; }
}

And then I just create the object:

    ExtendedItem ei = new ExtendedItem { Name = "MyName", Surname = "MySurname" };
    ExtendedContainer ec = new ExtendedContainer(ei);
    BaseContainer bc = ec;
    string temp = bc.item.Name;

it works now

  • 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-06-05T01:41:03+00:00Added an answer on June 5, 2026 at 1:41 am

    You’ve got two entirely separate properties here, which can store different values.

    You’ve set the ExtendedContainer.item property on the object, but you haven’t set the BaseContainer.item property.

    Imagine the two properties had entirely different names – you wouldn’t expect setting one to change the other then, would you? Well as far as the CLR is concerned, the fact that the two properties have the name is merely a coincidence.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 following classes: public class BaseProperty1{ public string Property1 {get; set;} }
I have following classes public class Person { public string FirstName { get; set;
I have the following (simplified) classes: public class Person { public string Id {get;
We have following classes public class MyPropertyBase { public int StartOffset { get; set;
I have the following classes: public class DictionaryBuilder<T> where T : IDictionary<string, object>, new()
I have the following classes: public class Person { public String FirstName { set;
I have the following classes: public class Student { public string StudentID { get;
I have the following classes: public class Note { public string Text { get;
Let's say I have the following classes : public class MyProductCode { private String
I have the following two classes: public class Address { public string AddressLine1 {

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.