对象在传递给C#中的属性时变为null

时间:2010-01-25 16:50:36

标签: c# properties null object

我有一个抽象类Employee和另外两个扩展它的类(Developer和Manager)。我的问题是,每当我创建一个管理器时

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

并尝试在新开发人员的经理字段中进行设置,

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

我一直收到我的经理为空的ArgumentException。我做了一些检查,当我尝试使用构造函数中的Manager属性设置它时,它会以某种方式变为null。任何关于我为什么会收到这个错误的建议都将不胜感激。 TIA!

每个代码如下:

//员工类

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

//开发人员类:

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

//经理类

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

4 个答案:

答案 0 :(得分:9)

你不想说:

if (value != null)

而不是

if (manager != null)

manager字段将初始化为null。 value关键字表示传递给属性的数据。

答案 1 :(得分:2)

更改

 if (manager != null) 

 if (value != null) 

答案 2 :(得分:1)

你永远不会设置字段管理器的值,只设置属性管理器,因此在检查管理器的值时,在属性中它是空的,因为它尚未设置。你可以在构造函数中设置字段管理器:

this.manager=manager

并检查属性中的值

if (value!=null)
{
     manager =value;
}

(无论如何你需要这样做,否则你从未使用过的财产价值永远不会被更新)

取决于您是否希望能够更改管理器。

答案 3 :(得分:1)

在Developer.Manager的setter change

if (manager != null)

if (value != null)