1

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

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();
 }

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

4

3 回答 3

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();
     }
于 2013-01-17T10:29:49.017 回答
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();
 }
于 2013-01-17T10:22:50.870 回答
1

导致问题的部分是这个

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

您正在使用在代码中调用的类Dates,并且在 C# 中这是一个引用类型。您正在代码中创建单个实例,并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
}
于 2013-01-17T10:35:39.040 回答