1

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 属性中创建了周三类的对象,但是为什么它向我显示错误我不知道我做错了什么,请解释....

谢谢,拉吉

4

2 回答 2

2

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

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

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

除此之外,我认为设计在各个方面都有些笨拙,但那是另一回事。

于 2012-07-24T09:49:00.620 回答
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);
    }
}
于 2012-07-24T09:57:50.173 回答