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

发布于 2024-08-19 11:04:23 字数 3721 浏览 4 评论 0原文

我有一个抽象类 Employee 和其他 2 个扩展它的类(开发人员和经理)。我的问题是,当我创建一个 Manager

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

并尝试将其设置在新开发人员的 Manager 字段中时,

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

我不断收到 ArgumentException ,表明我的 Manager 为空。我做了一些检查,当我尝试使用构造函数中的 Manager 属性设置它时,它不知何故变成了空。任何关于我为什么会收到此错误的建议将不胜感激。蒂亚!

每个类的代码如下:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蝶舞 2024-08-26 11:04:23

你不想说:

if (value != null)

而不是

if (manager != null)

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

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.

笛声青案梦长安 2024-08-26 11:04:23

更改

 if (manager != null) 

 if (value != null) 

Change

 if (manager != null) 

To

 if (value != null) 
臻嫒无言 2024-08-26 11:04:23

您从未设置字段管理器的值,仅设置属性管理器的值,因此在属性中,当您检查管理器的值时,它为空,因为它尚未设置。您可以在构造函数中设置字段管理器:

this.manager=manager

并检查属性中的值

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

(无论如何您都需要执行此操作,否则您赋予属性的值永远不会被使用并且永远不会更新)

取决于您是否想要管理器能够被改变。

you are never setting the value of the field manager, only the property Manager, so in the property when you check the value of manager it is null as it has not been set. you could set the field manager in the constructor:

this.manager=manager

and check for the value in the property

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

(you need to do this anyway, otherwise the value you give to the property is never used and will never be updated)

depending on if you want the Manager to be able to be changed.

生活了然无味 2024-08-26 11:04:23

在 Developer.Manager 的 setter 中更改

if (manager != null)

if (value != null)

In Developer.Manager's setter change

if (manager != null)

to

if (value != null)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文