尝试使用list<>创建用户定义的集合类

时间:2012-07-24 09:46:33

标签: c# class collections

C#:我正在尝试使用其他一些用户定义的类的列表创建集合类,但是当我尝试在主对象中添加对象时,它显示对象未设置为对象的实例。 这是我的C#代码:

namespace ConsoleApplication2
{

   public class Monthlst
    {
        public List<Monday> Monday { get; set; }
        public List<Tuesday> Tuesday { get; set; }
        public List<Wednesday> Wednesday { get; set; }
        public List<Thursday> Thursday { get; set; }
        public List<Friday> Friday { get; set; }
        public List<Saturday> Saturday { get; set; }
        public List<Sunday> Sunday { get; set; }

    }
   public class Monday
    {
        public int days { get; set; }
    }
   public class Tuesday
    {
        public int days { get; set; }
    }
   public class Wednesday
    {
        public int days { get; set; }
    }
   public class Thursday
    {
        public int days { get; set; }
    }
   public class Friday
    {
        public int days { get; set; }
    }
   public class Saturday
    {
        public int days { get; set; }
    }
   public class Sunday
    {
        public int days { get; set; }
    }
    class Program
    {


        static void Main(string[] args)
        {

          Monthlst objmonth = new Monthlst();
          Wednesday wednes = new Wednesday();
          wednes.days = 5;
          objmonth.Wednesday.Add(wednes); // here i am getting error object not set to.... 
        }
  }
}

这里我只是在main方法中创建我的Monthlst类的实例,并在Monthlst对象的list属性中创建星期三类的对象,但是为什么它显示错误我不知道我在做什么或者没有任何错误请解释。 ...

谢谢, 拉吉

2 个答案:

答案 0 :(得分:2)

是的,您没有初始化您的属性。构建完成后,所有Monthlst属性都将为null。您需要在添加列表之前创建列表。例如:

Monthlst objmonth = new Monthlst();
objmonth.Wednesday = new List<Wednesday>();
Wednesday wednes = new Wednesday();
wednes.days = 5;
objmonth.Wednesday.Add(wednes);

或者,使Monthlst的构造函数初始化其所有属性。

除此之外,我认为设计在各方面都有些不同,但这是另一回事。

答案 1 :(得分:0)

初始化您的Wednesday列表项。你的代码应该是这样的:

public class Monthlst
{
    public Monthlst()
    {
        Monday = new List<Monday>();
        Tuesday = new List<Tuesday>();
        Wednesday = new List<Wednesday>();
        Thursday = new List<Thursday>();
        Friday = new List<Friday>();
        Saturday = new List<Saturday>();
        Sunday = new List<Sunday>();
    }
    public List<Monday> Monday { get; set; }
    public List<Tuesday> Tuesday { get; set; }
    public List<Wednesday> Wednesday { get; set; }
    public List<Thursday> Thursday { get; set; }
    public List<Friday> Friday { get; set; }
    public List<Saturday> Saturday { get; set; }
    public List<Sunday> Sunday { get; set; }

}
public class Monday
{
    public int days { get; set; }
}
public class Tuesday
{
    public int days { get; set; }
}
public class Wednesday
{
    public int days { get; set; }
}
public class Thursday
{
    public int days { get; set; }
}
public class Friday
{
    public int days { get; set; }
}
public class Saturday
{
    public int days { get; set; }
}
public class Sunday
{
    public int days { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        var objmonth = new Monthlst();
        var wednes = new Wednesday {days = 5};
        objmonth.Wednesday.Add(wednes);
    }
}