简体   繁体   中英

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. You are creating a single instance in your code, and assign the reference called dt to it in the 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. 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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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