1

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..

9
  • what is dt variable, where is it declared Commented Jan 17, 2013 at 10:18
  • Also, what are start and end and where are they declared? Commented Jan 17, 2013 at 10:19
  • 1
    Why not list.Add(dt); ? Also where is list declared? Commented Jan 17, 2013 at 10:19
  • What is the type of list? Commented Jan 17, 2013 at 10:20
  • You really need to be more clear on what it is your trying to do otherwise no one will be able to help you out. Commented Jan 17, 2013 at 10:23

3 Answers 3

3

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();
     }
Sign up to request clarification or add additional context in comments.

3 Comments

Just to clarify what the actual fix is here. The problem with your original code is you re-using the same reference of Dates every time, you aren't creating new instances therefore when it goes to bind it's only ever binding one object. This is also the reason why you are seeing the last item in the list. This solution has redundant code Dates dt = new Dates().
@Pushpendra - you don't need int i = 0; - you don't use it anywhere
@Bridge--- actually int i=0 is used by DharaPPatel i have just copied her code and made the changes.
2

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

2 Comments

I have the same thing, downvoted with no explanation. Any bets that it might be the guy down below who got downvoted to -5 (not that I was one of those that downvoted them)?
me too, i never downvote with explanation we are here to help each other
1

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 type. 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
}

1 Comment

It sounds complicated, but it's obvious once you grok the concept.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.