简体   繁体   English

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

[英]Creating Custom Generic List in C#

Hello Friends i wanna create custom generic list my code is as follows : 你好朋友,我想创建自定义的通用列表,我的代码如下:

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

in my resultant list , it only shows last item added in loop please help solve the problem.. 在我的结果列表中,它仅显示循环中添加的最后一项,请帮助解决问题。

Try This :---- 尝试这个 : - -

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

Change this: 更改此:

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

to this and try: 为此,并尝试:

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

The part that is causing the problem is this 引起问题的部分是这个

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

You are using a class called Dates in your code, and in C# that is a reference typ e. 您在代码中使用的是名为Dates的类,在C#中使用的是引用类型 e。 You are creating a single instance in your code, and assign the reference called dt to it in the Dates dt = new Dates(); 您正在代码中创建一个实例,并在Dates dt = new Dates();为其分配名为dt的引用Dates dt = new Dates(); line. 线。
In the loop you change some properties of the instance, and add a reference to the instance to the list. 在循环中,您可以更改实例的某些属性,并将对该实例的引用添加到列表中。 Then the loop executes again, and you change the properties of the instance, thus changing the values of instance for the reference that is already in the list, and you add the same reference to the list again. 然后,循环再次执行,并且您更改了实例的属性,从而更改了列表中已有引用的实例值,然后将相同的引用再次添加到列表中。
The loops continues, as loops do, and this happens again and again, and you are left with a list that has a bunch of references to the exact same instance. 循环像循环一样继续进行,这一次又一次地发生,您将剩下一个列表,其中包含一堆对完全相同的实例的引用。

So the values of the list not just look the same, they are exactly the same thing. 因此, list的值不仅看起来相同,而且是完全一样的。 To solve this, you would need to create a new instance of the Dates class each time you need to add an instance to the list, with code like this. 为了解决这个问题,每次需要使用以下代码将实例添加到列表时,都需要创建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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM