Share via


C# How do I instantiate a nested class within its parent class?

Question

Sunday, August 20, 2017 10:45 PM

Big question about nested classes!

I have the following three classes:

public class Level1Customer
{    
    public string CustomerFirstName { get; set; }    
    public string CustomerLastName { get; set; }    
    public string CustomerAcctNumber { get; set; }    
    public clsCustomerInfo CustomerInfo = new clsCustomerInfo();
}

public class Level2Customer
{    
    public string CustomerFirstName { get; set; }    
    public string CustomerLastName { get; set; }   
    public string CustomerAcctNumber { get; set; }    
    public clsCustomerInfo CustomerInfo = new clsCustomerInfo();
}

public class clsCustomerInfo
{    
    public string AcctCreationDate { get; set; }    
    public string AcctExpirationDate { get; set; }    
    public int PurchasesToDate { get; set; }
}

Notice the instance of clsCustomerInfo in the Level1Customer and Level2Customer classes. What I'm hoping to accomplish with is to instantiate the clsCustomerInfo class automatically so it exists whenever the parent Level1Customer or Level2Customer class is instantiated. The code above produces a Null Reference exception when I try to write a value to the clsCustomerInfo members.

For example:

Level1Customer.CustomerInfo.PurchasesToDate = txtbxTotalPurchases.Text.ToString();

So how can I ensure that the nested class clsCustomerInfo will actually exist when its parent class Level1Customer is created in code? When I write this in code...

Level1Customer Lev1Customer = new Level1Customer();

...I want that CustomerInfo class inside of it to exist as well when I try to set the value of CustomerInfo.PurchasesToDate.

All replies (3)

Sunday, August 20, 2017 11:35 PM

Greetings MB.

What you are doing looks correct to me. I get no error using your classes in the following code.

      static public void Main(string[] args)
      {
         Level1Customer cust = new Level1Customer();
         cust.CustomerInfo.PurchasesToDate = 100;

         Console.WriteLine(cust.CustomerInfo.PurchasesToDate);
      }

Are you sure it's not txtbxTotalPurchases that's null? (Oh, and by the way, the Text of a TextBox is already a string, so you don't need to call its ToString() method.)

Edit: I just realised you are trying to assign a string to an int, which won't work. But I would have expected a different error, along the lines of "Cannot implicitly convert..." at compile time, not a null reference error at runtime.


Monday, August 21, 2017 12:11 AM

public class Level1Customer
{    
    private clsCustomerInfo custinfo = new clsCustomerInfo();

    public string CustomerFirstName { get; set; }    
    public string CustomerLastName { get; set; }    
    public string CustomerAcctNumber { get; set; }  

    public clsCustomerInfo CustomerInfo
    {
       get{return custinfo;}
       set{custinfo = value;}
    }
}

The above is how I have done it for years so when the parent is instantiated new,  the child in the parent is instantiated new too within the parent and the pubic property uses its backing private data stor, which I learned from a MDSN C# certification book many many years ago. 


Monday, August 21, 2017 2:08 AM

Hello Broad,

>>The code above produces a Null Reference exception when I try to write a value to the clsCustomerInfo members.

I have tested your code and everything works well. when I execute the following code,

Level1Customer Lev1Customer = new Level1Customer();

it shows that the nested class has been instantiated.

Maybe you could share more information about your error message so that I could do further test.

Best regards,

feih_7

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].