Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objects becoming null when passed to a Property in C#

I have an abstract class Employee and 2 other classes that extend it (Developer and Manager). My problem is that when I whenever I create a Manager

Employee man = new Manager(1234567, 30, "Bob", "Pie")

and try to set it in the Manager field of a new Developer,

Employee codemonkey = new Developer(1234568, 20, "Code", "Monkey", (Manager)man)

I keep getting the ArgumentException that my Manager is null. I did some checks and it somehow becomes null when I try to set it with the Manager property in the constructor. Any advice as to why I'm getting this error would be greatly appreciated. TIA!

The code for each is below:

//Employee Class

public abstract class Employee
{
    string firstName, lastName;
    int id, yearsEmployed;

    //Names must be non-empty
    public string FirstName
    {
        get { return firstName; }
        set
        {
            if (!value.Equals(""))
            {
                firstName = value;
            }
            else
                throw new ArgumentException("name cannot be empty");
        }
    }
    public string LastName
    {
        get { return lastName; }
        set
        {
            if (!value.Equals(""))
            {
                lastName = value;
            }
            else
                throw new ArgumentException("name cannot be empty");
        }
    }
    // IDs must be strings consisting of exactly seven digits.
    public int ID
    {
        get { return id; }
        set
        {
            if (value.ToString().Length == 7)
            {
                id = value;
            }
            else
                throw new ArgumentException("ID must consist of 7 digits");
        }
    }
    // Years employed must always be non-negative.
    public int YearsEmployed
    {
        get { return yearsEmployed; }
        set
        {
            if (value >= 0)
            {
                yearsEmployed = value;
            }
            else
                throw new ArgumentException("Year employed must be non-negative");
        }
    }
    //Constructor
    public Employee(int id, int yearsEmployed,
                    string firstName, string lastName)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
        this.ID = id;
        this.YearsEmployed = yearsEmployed;
    }
    public abstract int GetLevel { get; }
    public abstract string GetTitle { get; }
    public string GetFullTitle { get { return GetTitle + " " + GetLevel; } }
}

//Developer class:

 public class Developer : Employee
{
    Manager manager;

    //Manager cannot be null
    public Manager Manager
    {
        get { return manager; }
        set
        {
            if (manager != null)
            {
                manager = value;
            }
            else
                throw new ArgumentException("Manager cannot be null");
        }
    }

    //Constructor
    public Developer(int id, int yearsEmployed, string firstName,
                    string lastName, Manager manager)
        : base(id, yearsEmployed, firstName, lastName)
    {
        Console.WriteLine("manager is not null:" + manager != null); //True here
        this.Manager = manager; // manager is null here
    }

    public override int GetLevel
    {
        get { return (this.YearsEmployed + 1) / 3; }
    }

    public override string GetTitle
    {
        get { return "Developer"; }
    }
}

//Manager Class

public class Manager : Employee
{
    //Constructor
    public Manager(int id, int yearsEmployed,
                    string firstName, string lastName)
        : base(id, yearsEmployed, firstName, lastName) { }

    public override int GetLevel
    {
        get { return (YearsEmployed + 1) / 2; }
    }

    public override string GetTitle
    {
        get { return "Manager"; }
    }
}
like image 354
Mel Avatar asked Jan 25 '10 16:01

Mel


2 Answers

Don't you want to say:

if (value != null)

instead of

if (manager != null)

The manager field will be initialized to null. The value keyword represents the data that is being passed to the property.

like image 141
Adam Crossland Avatar answered Oct 13 '22 11:10

Adam Crossland


Change

 if (manager != null) 

To

 if (value != null) 
like image 20
Klaus Byskov Pedersen Avatar answered Oct 13 '22 09:10

Klaus Byskov Pedersen