C# 嵌套类型列表

发布于 2024-07-30 03:05:57 字数 1742 浏览 4 评论 0原文

我创建了一个包含类型列表的类,并派生到我创建的另一个类。 如下所示:

namespace MyIntegretyCheck.Common
{
    /// <summary>
    /// Description of PolicyErrors.
    /// </summary>
    public partial class PolicyErrorEndingDates
    {
        public int ID_P {get;set;}
        public DateTime DT_S_E {get;set;}
        public DateTime DT_SC_E {get;set;}

        public List<PolicyErrorDescr> Errors {get;set;}
    }

    public partial class PolicyErrorDescr
    {
        public string Field1{get;set;}
        public string Field2 {get;set;}
        public string F1IsThanF2 {get;set;}
        public string Message {get;set;}
        public int ErrorLevel {get;set;} //0= Info | 1= Warning | 2= Error

    }
}

现在我创建了一个 PolicyErrorEndingDates 的类型列表,添加了一个条目,然后尝试添加其嵌套列表的条目错误如下:

public List<PolicyErrorEndingDates> MyPolicyEndingDates()
{

    DAL.PolicyEndingDates ped = new DAL.PolicyEndingDates();
    List<PolicyErrorEndingDates> MyErrors = new List<PolicyErrorEndingDates>();

    foreach(var m in ped.CheckTables())
    {
        bool HasError = false;
        PolicyErrorEndingDates p = new PolicyErrorEndingDates();
        p.ID_P = m.ID_P;

        if(m.DT_S_E != m.DT_SC_E)
        {
            PolicyErrorDescr e = new PolicyErrorDescr();
            HasError = true;
            e.Field1 = "DT_S_E";
            e.Field2 = "DT_SC_E";
            e.Message = "blablabla...";
            e.ErrorLevel = 3;
            p.Errors.Add(e);
        }

        if(HasError)
            MyErrors.Add(p);
    }
}

调试器崩溃,并显示消息 Object reference not设置为对象的实例,位于我的 if 内的 p.Errors.Add(e); 行。 我做错了什么? 如何创建嵌套列表的实例?

I've created a class witch contains a typed list and derives to another class that I created. This looks as follows:

namespace MyIntegretyCheck.Common
{
    /// <summary>
    /// Description of PolicyErrors.
    /// </summary>
    public partial class PolicyErrorEndingDates
    {
        public int ID_P {get;set;}
        public DateTime DT_S_E {get;set;}
        public DateTime DT_SC_E {get;set;}

        public List<PolicyErrorDescr> Errors {get;set;}
    }

    public partial class PolicyErrorDescr
    {
        public string Field1{get;set;}
        public string Field2 {get;set;}
        public string F1IsThanF2 {get;set;}
        public string Message {get;set;}
        public int ErrorLevel {get;set;} //0= Info | 1= Warning | 2= Error

    }
}

Now I created a typed list of PolicyErrorEndingDates, added an entry and tried then to add entries of his nested list Errors as follows:

public List<PolicyErrorEndingDates> MyPolicyEndingDates()
{

    DAL.PolicyEndingDates ped = new DAL.PolicyEndingDates();
    List<PolicyErrorEndingDates> MyErrors = new List<PolicyErrorEndingDates>();

    foreach(var m in ped.CheckTables())
    {
        bool HasError = false;
        PolicyErrorEndingDates p = new PolicyErrorEndingDates();
        p.ID_P = m.ID_P;

        if(m.DT_S_E != m.DT_SC_E)
        {
            PolicyErrorDescr e = new PolicyErrorDescr();
            HasError = true;
            e.Field1 = "DT_S_E";
            e.Field2 = "DT_SC_E";
            e.Message = "blablabla...";
            e.ErrorLevel = 3;
            p.Errors.Add(e);
        }

        if(HasError)
            MyErrors.Add(p);
    }
}

The Debugger crashed with the message Object reference not set to an instance of an object, at the line p.Errors.Add(e); inside my if. What did I do wrong? How can I create an instance of the nested list?

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

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

发布评论

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

评论(4

你不是我要的菜∠ 2024-08-06 03:05:57

您是否将 List 实例分配给 Errors 属性?

首先,我可能会将 Errors 集合从外部设置为只读,即可以更改列表,但不能为其提供新列表。

我将通过将 setter 设置为私有来实现:

public List<PolicyErrorDescr> Errors { get; private set; }
                                            ^-----^
                                              this

然后我将创建一个构造函数并从中将一个集合实例分配给该属性:

public PolicyErrorEndingDates()
{
    Errors = new List<PolicyErrorDescr>();
}

这应该处理空引用异常。

Did you assign a List<PolicyErrorDescr> instance to the Errors property?

First, I would probably make the Errors collection read-only from the outside, that is, the list can be changed, but you can't give it a new list.

This I would do by making the setter private:

public List<PolicyErrorDescr> Errors { get; private set; }
                                            ^-----^
                                              this

Then I would create a constructor and assign a collection instance to that property from it:

public PolicyErrorEndingDates()
{
    Errors = new List<PolicyErrorDescr>();
}

This should take care of the null reference exception.

慈悲佛祖 2024-08-06 03:05:57

我的猜测是,由于 p 是一个新实例,因此错误列表没有实例化(就像 Lasse 提到的那样)。

e.ErrorLevel = 3;
p.Errors = new List<PolicyErrorDescr>(); //add this
p.Errors.Add(e);

My guess, since p is a new instance, the Errors list wasn't instantiated (Like what Lasse mentioned).

e.ErrorLevel = 3;
p.Errors = new List<PolicyErrorDescr>(); //add this
p.Errors.Add(e);
无敌元气妹 2024-08-06 03:05:57

永远不会初始化 PolicyErrorEndingDates 中的错误列表:

如果按如下方式更正,则

public partial class PolicyErrorEndingDates
    {
        public int ID_P {get;set;}
        public DateTime DT_S_E {get;set;}
        public DateTime DT_SC_E {get;set;}

        public List<PolicyErrorDescr> Errors {get;set;}
        public PolicyErrorEndingDates()
        {
            Errors = new List<PolicyErrorDescr>()
        }
    }

You never initialise the Errors list in the PolicyErrorEndingDates

if you correct as follows:

public partial class PolicyErrorEndingDates
    {
        public int ID_P {get;set;}
        public DateTime DT_S_E {get;set;}
        public DateTime DT_SC_E {get;set;}

        public List<PolicyErrorDescr> Errors {get;set;}
        public PolicyErrorEndingDates()
        {
            Errors = new List<PolicyErrorDescr>()
        }
    }
冰葑 2024-08-06 03:05:57

p.Errors 为空

p.Errors is null

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