jhon.smith
jhon.smith

Reputation: 2043

Initializing nested object properties

I have a class called employee which has a field called insurance which is of type insurance like this

public class Employee
{
    public string Name;
    public Insurance Insurance;
}

I have another class called Insurance

public class Insurance
{
    public int PolicyId;
    public String PolicyName;
} 

Now in the main program i want to do something like

var myEmployee = new Employee();
myEmployee.Name = "Jhon";
myEmployee.Insurance.PolicyId = 123 ;
myEmployee.Insurance.PolicyName = "Life Time" ;

C# is complaining and i know how to fix it by creating a instance of the Insurance class.

My question is can i somehow assign the values for the fields in the way i want to do it in the main program using like

**

myEmployee.Insurance.PolicyId = 123 ;
myEmployee.Insurance.PolicyName = "Life Time" ;

** I tried

 public class Employee
    {

        public Employee()
        {
            Insurance Insurance = new Insurance();
        }

        public String Name;
        public Insurance Insurance;



        public class Insurance
        {
            public int PolicyId;
            public String PolicyName;
        } 
    }

In the main method when i try

class Program
    {
        static void Main(string[] args)
        {
            var joe = new Employee();
            joe.Name = "Joe";
            joe.Insurance.

        }

I get this error-

Error 2 Ambiguity between 'ConsoleApplication1.Employee.Insurance' and 'ConsoleApplication1.Employee.Insurance' c:\users\lenovo\documents\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs 15 17 ConsoleApplication1

Upvotes: 12

Views: 29307

Answers (4)

bland
bland

Reputation: 2004

You could instantiate Insurance in Employee's constructor so it is done automatically for you. You could provide it default values to ensure it is understood that is not yet defined to be valid when accessed later on.

public class Employee
{
    Insurance Insurance { get; set; }

    public Employee()
    {
        this.Insurance = new Insurance() { PolicyId = -1 };
    }
}

public class Insurance
{
    public int PolicyId { get; set; }
    public string PolicyName { get; set; }
}

Or to keep the classes nested:

public class Employee
{
    Insurance InsurancePolicy { get; set; }

    public Employee()
    {
        this.InsurancePolicy = new Insurance() { PolicyId = -1 };
    }
    public class Insurance
    {
        public int PolicyId { get; set; }
        public string PolicyName { get; set; }
    }
}

Upvotes: 17

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107626

Without requiring changes to your Employee class, you could use object initializers:

var myEmployee = new Employee 
{
    Name = "Jhon",
    Insurance = new Insurance
    {
        PolicyId = 123,
        PolicyName = "Life Time"
    }
};

Alternatively, and maybe preferably, you can have the Employee class create a new instance of Insurance either in its constructor (as in the other answers), or one other option would be to do it in the Insurance property getter itself, so it's instantiated only if you use it. Here's an example of the latter:

class Employee 
{
    private Insurance insurance;

    public Insurance Insurance
    {
        get
        {
            if (insurance == null)
            {
                insurance = new Insurance();
            }
            return insurance;
        }
    }
}

Lastly, I would suggest that you don't build classes that have all public fields unless you really know that's what you want. Instead, I would consider using properties over fields. I have incorporated other's suggestions into the following code, and provided my own:

public class Employee
{
    public Employee() 
    {
        this.Insurance = new Insurance();
    }

    // Perhaps another constructor for the name?
    public Employee(string name)
        : this()
    {
        this.Name = name;
    }

    public string Name { get; set; }
    public Insurance Insurance { get; private set; }
}

public class Insurance
{
    public int PolicyId { get; set; }
    public string PolicyName { get; set; }
}

Upvotes: 12

KappaG3
KappaG3

Reputation: 671

Of course, but how are you going to assign something that is belonging to a null object? You need to instantiate Insurance in Employee's constructor.

public Employee()
{
     this.Insurance = new Insurance();
}

EDIT Regarding your comment: Following this approach you will be able to access myEmplyee.Insurance.PolicyID with two dots. The constructor is inside Employee's class, so you won't have to type anything more than what you already tried to do, once you implement it.

Upvotes: 6

You can write a constructor for your employee that will instantiate Insurance

public class Employee
{
    public Employee()
    {
        this.Insurance = new Insurance();
    }
    public string Name;
    public Insurance Insurance;
}

Upvotes: 1

Related Questions