在C#中创建自定义通用列表

时间:2013-01-17 10:16:50

标签: c# asp.net-mvc c#-4.0 generics generic-collections

你好朋友我想创建自定义通用列表我的代码如下:

public class Dates
{
    string _FromDate;
    string _ToDate;

    public string FromDate
    {
        get { return _FromDate; }
        set { _FromDate = value; }
    }

    public string ToDate
    {
        get { return _ToDate; }
        set { _ToDate = value; }
    }
}

protected void btnsearch_Click(object sender, EventArgs e)
{

    DateTime start = new DateTime(2013,1,5);
    DateTime end = new DateTime(2013,2,2);

    string dayName = drpday.SelectedItem.ToString().ToLower();

     Dates dt = new Dates();
    List<Dates> list = new List<Dates>();
    int i = 0;

   for (DateTime runDate = start; runDate <= end; runDate = runDate.AddDays(1))
    {
        if (runDate.DayOfWeek.ToString().ToLower() == dayName)
        {
            dt.FromDate = runDate.ToShortDateString();
            dt.ToDate = (runDate.AddDays(double.Parse(hd_tourdays.Value)).ToShortDateString());
            list.Insert(i++,dt);
        }
    }
     grd_TourDates.DataSource = list;
     grd_TourDates.DataBind();
 }

在我的结果列表中,它只显示循环中添加的最后一项,请帮助解决问题..

3 个答案:

答案 0 :(得分:3)

试试这个:----

protected void btnsearch_Click(object sender, EventArgs e)
    {

        DateTime start = new DateTime(2013,1,5);
        DateTime end = new DateTime(2013,2,2);

        string dayName = drpday.SelectedItem.ToString().ToLower();

         Dates dt = new Dates();
        List<Dates> list = new List<Dates>();
        int i = 0;

       for (DateTime runDate = start; runDate <= end; runDate = runDate.AddDays(1))
        {
            if (runDate.DayOfWeek.ToString().ToLower() == dayName)
            {

                list.Add(new Dates{
                      FromDate=runDate.ToShortDateString();
                      ToDate=(runDate.AddDays(double.Parse(hd_tourdays.Value)).ToShortDateString());
    });

            }
        }
         grd_TourDates.DataSource = list;
         grd_TourDates.DataBind();
     }

答案 1 :(得分:2)

改变这个:

protected void btnsearch_Click(object sender, EventArgs e)
{
     Dates dt = new Dates();
    List<Dates> list = new List<Dates>();
    int i = 0;

   for (DateTime runDate = start; runDate <= end; runDate = runDate.AddDays(1))
    {
        if (runDate.DayOfWeek.ToString().ToLower() == dayName)
        {
            dt.FromDate = runDate.ToShortDateString();
            dt.ToDate = (runDate.AddDays(double.Parse(hd_tourdays.Value)).ToShortDateString());
            list.Insert(i++,dt);
        }
    }
     grd_TourDates.DataSource = list;
     grd_TourDates.DataBind();
 }

到此并尝试:

protected void btnsearch_Click(object sender, EventArgs e)
{
     Dates dt;
    List<Dates> list = new List<Dates>();
    int i = 0;

   for (DateTime runDate = start; runDate <= end; runDate = runDate.AddDays(1))
    {
        if (runDate.DayOfWeek.ToString().ToLower() == dayName)
        {
            dt = new Dates()
            dt.FromDate = runDate.ToShortDateString();
            dt.ToDate = (runDate.AddDays(double.Parse(hd_tourdays.Value)).ToShortDateString());
            list.Insert(i++,dt);
        }
    }
     grd_TourDates.DataSource = list;
     grd_TourDates.DataBind();
 }

答案 2 :(得分:1)

造成问题的部分是这个

Dates dt = new Dates();
for (.....)
{
  dt.FromDate = ...;
  dt.ToDate = ...;
  list.Insert(i++,dt);
}

您在代码中使用了名为Dates的类,而在C#中使用的是a reference typ e。您正在代码中创建单个实例,并在dt行中为其指定名为Dates dt = new Dates();的引用。
在循环中,您可以更改实例的某些属性,并将引用添加到列表中的实例。然后循环再次执行,您更改实例的属性,从而更改列表中已有的引用的实例值,并再次将相同的引用添加到列表中。登记/> 循环继续,就像循环一样,这种情况一次又一次地发生,并且你留下了一个列表,其中有一堆对完全相同的实例的引用。

因此list的值不仅仅看起来相同,它们完全相同的东西。要解决此问题,每次需要将实例添加到列表时,您需要创建Dates类的新实例,其代码如下所示。

for (.....)
{
  Dates dt = new Dates(); //creates a new reference to a new instance
  dt.FromDate = ...;      //sets properties on the instance
  dt.ToDate = ...;
  list.Insert(i++,dt);    // inserts a reference to the instance in the list
}