【问题标题】:list class property in a class c# assign value列出类中的类属性 c# 赋值
【发布时间】:2014-08-04 09:36:22
【问题描述】:
class Program
{
    static void Main(string[] args)
    {
        Posting onjPosting = null;
        List<Posting> objList = null;

        for (int i = 0; i < 100; i++)
        {
            onjPosting = new Posting();
            onjPosting.Key1 = i;
            for (int j = 0; j < 5; i++)
            {
                Choice objChoice = new Choice();
                objChoice.ID = i;
                objChoice.VAL = j;

                onjPosting.GetPostingChoice.Add(objChoice); // GETTING ERROR [ Object reference not set to an instance of an object. ] 

            }
            objList.Add(onjPosting);
        }
    }
}


public class Choice
{
    public int ID { get; set; }
    public int VAL { get; set; }
}    
public class Posting
{

    public int Key1 { get; set; }        
    public List<Choice> GetPostingChoice { get; set; }

}

在循环并分配值时出现错误。如何解决这个问题?请帮帮我。

我的要求是一个父类(Posting),可以包含多个数据 List 。

提前致谢。

【问题讨论】:

  • GetPostingChoice 为空。使用前先初始化……更好,学习如何调试程序……
  • 什么错误?你赋予什么价值?
  • 顺便说一句。调用属性Get... 不利于代码的可读性。人们会期望一种方法而不是属性
  • 错误,对象引用未设置为对象的实例。你能帮我看看创建实例的语法吗?
  • 你能分享一个例子吗?

标签: c# asp.net winforms c#-4.0 console


【解决方案1】:

您从不分配GetPostingChoice 列表,所以它当然是空的。

你可以在构造函数中做到这一点:

public class Posting
{
    public Posting()
    {
        GetPostingChoice = new List<Choice>();
    }

    public int Key1 { get; set; }        
    public List<Choice> GetPostingChoice { get; set; }
}

【讨论】:

    【解决方案2】:

    Posting 类上添加公共构造函数:

    public class Posting
    {
    
        public int Key1 { get; set; }        
        public List<Choice> GetPostingChoice { get; set; }
        public Posting()
        {
            GetPostingChoice = new List<Choice>();
        }
    }
    

    您还有其他错误:

    1. 你没有初始化objList,所以你不能在那里添加。

      List<Posting> objList = null;
      

      所以当你到达时你会得到另一个空引用:

      List<Posting> objList = null;
      
    2. 在你的第二个循环中你增加 i 而不是 j 所以它永远不会结束。

      for (int j = 0; j < 5; i++)
      

    它应该是这样的:

    Posting onjPosting = null;
    List<Posting> objList = new List<Posting>();
    
    for (int i = 0; i < 1; i++)
    {
        onjPosting = new Posting();
        onjPosting.Key1 = i;
        for (int j = 0; j < 5; j++)
        {
            Choice objChoice = new Choice();
            objChoice.ID = i;
            objChoice.VAL = j;
    
            onjPosting.GetPostingChoice.Add(objChoice); // GETTING ERROR [ Object reference not set to an instance of an object. ] 
    
        }
        objList.Add(onjPosting);
    }
    

    既然您要求另一种方法,而这只是您可以做到的众多方法之一,请看一下:

    List<Posting> objList = new List<Posting>();
    Enumerable.Range(0,100)
    .Select
    (
        (x,i)=>new Posting
        {
            Key1 = i,
            GetPostingChoice = Enumerable.Range(0,5).Select((p,j)=>new Choice{ID = i,VAL = j}).ToList()
        }
    );
    

    【讨论】:

    • 有很多方法可以做同样的想法。好的对你意味着什么?
    • 您能再分享一种方法吗?让我有更好的理解。
    • 更好地理解什么?你所做的非常清晰和简单。还有其他方法吗?是的,有,但您想了解什么?
    • 实际上我将在 WCF 上实现相同的功能。那么你能告诉我它的性能吗?由于他们是其他方法也可用。如果可能,请再分享一种方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 2018-10-03
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多