12

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

2
  • I want to use myEmployee.Insurance.PolicyId using 2 dots.Is this even possible in any object oriented langauge . Commented Aug 8, 2013 at 18:37
  • 1
    You defined a class "Insurance" and a property "Insurance" both within Employee. Must be uniquely named within the class, otherwise it won't know which to work on. Make Insurance it's own class, or rename one of them. I updated my answer to reflect your edits and resolve this. Commented Aug 8, 2013 at 20:05

4 Answers 4

17

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; }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

13

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; }
}

3 Comments

That singleton-like approach doesn't sound very useful in this case. Instantiating in the constructor is more than enough, and it also avoids a null-check every time Insurance is trying to be accessed.
It would be better if the Insurance class was a heavy object, though. I'll give you that.
@KappaG3: I agree 100%, just wanted to show another possible approach.
6

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.

1 Comment

I generally do this and prefer it over any other ways. It works great for instantiating collections.
1

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;
}

4 Comments

this keyword is redundant unless used for extension methods.
It avoids confusion when the member's name is the same as its type.
@HighCore the this keyword is more expressive, and it is useful when you're trying to read more complex code. especially when you're reading code that you yourself didn't write
@jhon.smith the error you're getting is because there are 2 meanings to Employee.Insurance You might want to move your Insurance class to outside of Employee

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.