Home > front end >  Xamarin/C# - Method to find weekends in a date range. Why am I stuck in loop and unable to increment
Xamarin/C# - Method to find weekends in a date range. Why am I stuck in loop and unable to increment

Time:10-06

Problem: I have a page in my Xamarin project that takes two user inputted dates and checks for all the Saturdays and Sundays in that range, adding all these dates to a list which I can then count and deduct from a day total. When I run the second datepicker, the screen will freeze and crash the app after roughly a minute. My output window was giving me LOS overflow messages. When I put a breakpoint on the 'getWeekendCount()' method, I find that the AddDays(1) method is not incremementing the value of 'date' and seems to be looping over the same date which I believe is causing the overflow issue.

I can't see why this would be happening and would appreciate any help.

public Calculator()
    {
        InitializeComponent();
        
        //OnDateSelected(null, null);
        dateFrom.SetValue(DatePicker.MinimumDateProperty, DateTime.Now);
        dateTo.SetValue(DatePicker.MinimumDateProperty, DateTime.Now);
    }

    public int getWeekendCount(DateTime a, DateTime b)
    {
        List<DateTime> allDates = new List<DateTime>();
        Console.WriteLine("Date From: "   a.Date);
        Console.WriteLine("Date To: "   b.Date);
        for (DateTime date = a.Date; date <= b.Date; date.AddDays(1))
        {
            if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
            {
                allDates.Add(date);
                Console.WriteLine(allDates);
            }
        }

        int weekendCount = allDates.Count();

        return weekendCount;
    }
   
    void OnDateSelected(object sender, DateChangedEventArgs e)
    {
        
        if(dateTo.Date < dateFrom.Date)
        {
            leaveDaysLabel.Text = "Days: Invalid Selection";
        }
        else if (dateTo.Date == dateFrom.Date)
        {
            leaveDaysLabel.Text = "Days: 1";
        }
        else
        {
            Console.WriteLine("Date From: "   dateFrom.Date);
            Console.WriteLine("Date To: "   dateTo.Date);
            
            int count = getWeekendCount(dateFrom.Date, dateTo.Date);
            Console.WriteLine(count);

            int days = (dateTo.Date - dateFrom.Date).Days - count;

            leaveDaysLabel.Text = String.Format("Days: {0}", days);

            leaveHoursLabel.Text = "Hours: "   (days * 7.4).ToString();
        }

    }

CodePudding user response:

If you check the AddDays definition you will see that it does not modify your variable. Only returns a new date. So your loop is going on forever

So you should change your loop. An example

    while(a.Date <= b.Date)
    {
        if (a.DayOfWeek != DayOfWeek.Saturday && a.DayOfWeek != DayOfWeek.Sunday)
        {
            allDates.Add(a);
            Console.WriteLine(allDates);
        }
         a = a.AddDays(1);
    }

Also, maybe you should only call WriteLine on the date a instead of the whole list. Or print the whole list after the loop.

Happy coding!

  • Related