继承的Form.Text-对象引用未设置为对象的实例-自定义类

本文关键字:实例 对象 自定义 设置 Form Text- 对象引用 继承 | 更新日期: 2023-09-27 18:25:56

所以我正在处理表单继承,并覆盖基表单的.Text()属性。除了尝试调用自定义类属性外,继承的一切都很好。

public partial class baseForm : Form
{
    public baseForm() { InitializeComponent(); }
    public override string Text
    {
        get
        {
            return base.Text;
        }
        set
        {
            base.Text = value + " - " + customClass.employee.userId;
        }
    }
}

当我通过派生类的设计器启用我的代码customClass.employe.userId时,我会中断,并收到一条错误消息"Object reference not set to a instance of a Object"。我的代码编译时没有出错,在运行过程中,甚至按计划正确执行。结果就在那里,但我需要设计师。

在开发过程中,我如何让设计师回来,而不必一直注释这一行,然后在生产中取消注释?

编辑中的customClass在整个应用程序的运行时在登录表单中实例化。在登录期间,在frmLogin.cs中,成功登录后,我设置类的对象。

customClass.employee = new customClass.clsEMPLOYEEModel(connectionString, userId.Text);

public static class customClass
{
    public static clsDynamicClass.clsEMPLOYEEModel employee
    {
        get
        {
            return _employee;
        }
        set
        {
            _employee = value;
        }
    }
}

继承的Form.Text-对象引用未设置为对象的实例-自定义类

在整个应用的运行时,在登录表单中实例化的customClass

这意味着employee属性未设置为设计时。我建议在构造函数中使用"dummy"值初始化employee,或者在setter:中检查null

public override string Text
{
    get
    {
        return base.Text;
    }
    set
    {
        if(customClass != null && customClass.employee != null)
            base.Text = value + " - " + customClass.employee.userId;
        else
            base.Text = value;
    }
}