0

I would imagine this is quite a basic question in C#. I've got my head in a bit of spin with it though and I'm unsure of the correct way of sorting it.

I have a parent class with get/set properties and with a child class. When an instance of the class is created using new the property to the parent class is accessible but the child class isn't. I remember in C programming you have to create the memory space for this but I'm unsure of the correct way of doing this in C#.

Parent Class

class Parent_class
{
    private int number;
    public int Number
    {
        get { return number; }
        set { number = value; }
    }
    private Child_class childclass;// = new Child_class();
    public Child_class Childclass
    {
        get { return childclass; }
        set { childclass = value; }
    }
}

Child Class

class Child_class
{
    private int number;
    public int Number
    {
        get { return number; }
        set { number = value; }
    }
}

Main

    static void Main(string[] args)
    {
        Parent_class test = new Parent_class();
        test.Number = 3;            //<--Ok
        test.Childclass.Number = 4; //<--NullReferenceException
    }
  • You have created an instance of "Parent_class" to use that class. So where is your "Child_class" instance to use that class? – Lost_In_Library Jul 08 '12 at 03:31
  • possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders Jul 08 '12 at 03:36

2 Answers2

3

You don't need to use the field-backed getter / setter if you aren't doing anything special -- the compiler can create that for you.

To obtain an instance of a class, you need to use new. Since it looks like you want Parent_class to have an instance of child class automatically, you can do that in the constructor.

Oh - and the reason why Number works fine is that's a primitive type, and not a class. Primitives, (int, float, bool, double, DateTime, TimeSpan, to name a few) do not need instantiation via new.

Parent Class

public class Parent_class
{
    public Parent_class()
    {
      Childclass = new Child_class();
    }
    public int Number { get; set; }
    public Child_class Childclass { get; set; }
}

Child Class

public class Child_class
{
    public int Number { get; set; }
}

Main

static void Main(string[] args)
{
    Parent_class test = new Parent_class();
    test.Number = 3;            //<--Ok
    test.Childclass.Number = 4;
}
William Melani
  • 4,270
  • 1
  • 19
  • 43
0

You have not created Instance of Child class.

You can do either of below

  1. Initialize just before use

    static void Main(string[] args)
    {
        Parent_class test = new Parent_class();
        test.Number = 3;            //<--Ok
        test.ChildClass = new Child_class(); 
        test.Childclass.Number = 4; //<--NullReferenceException
    }
    

    2 . Initialize in the parent ctor

        public Parent_class()
        {
        Childclass = new Child_class();
        }
    

3 . Initialize inline at the time of declaration

   private Child_class childclass = new Child_class();
Tilak
  • 27,830
  • 17
  • 73
  • 125