Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C# Windows form app I have errors that i do not understand. Can someone please h

ID: 3920244 • Letter: C

Question

C# Windows form app

I have errors that i do not understand. Can someone please help me fix the issue.
Im using the dateTimePicker to grab its dates and convert them to strings and get the dates between and all store them in a list. Once in the datesbetween list im using them to filter files in a directory by the file name which is the date (ex. 20180323_mercedes.csv). I need help with following errors.


List<DateTime> DatesBetween(DateTime earliestDate, DateTime latestDate)
A local or parameter named 'earliestDate' cannot be declared in this scope because that name is used in an enclosing local scope to defined a local or parameter
A local or parameter named 'latestDate' cannot be declared in this scope because that name is used in an enclosing local scope to defined a local or parameter    
return allDates.ToString();
Cannot implicitly convert type 'string' to 'System.Collections.Generic.List<System.DateTime>'   

  
**********************************************************************************************************  
syntax :


  private void button2_Click(object sender, EventArgs e)
        {
   string earliestdate = dateTimePicker1.Value.ToString("yyyyMMdd");
            string latestdate = dateTimePicker2.Value.ToString("yyyyMMdd");
            DateTime earliestDate = DateTime.ParseExact(earliestdate, "yyyyMMdd", CultureInfo.InvariantCulture);
            DateTime latestDate = DateTime.ParseExact(latestdate, "yyyyMMdd", CultureInfo.InvariantCulture);

            List<DateTime> DatesBetween(DateTime earliestDate, DateTime latestDate)
            {
                List<DateTime> allDates = new List<DateTime>();
                for (DateTime date = earliestDate; date <= latestDate; date = date.AddDays(1))
                    allDates.Add(date);
                return allDates.ToString();
            }
  List<string> fileDirectories = new List<string>();

            if (checkedListBox1.GetItemCheckState(0) == CheckState.Checked)
            {
                if (checkedListBox2.GetItemCheckState(0) == CheckState.Checked)
    {
     
     fileDirectories.Add("...................")
       }
    else if (checkedListBox2.GetItemCheckState(1) == CheckState.Checked)
                {
                    fileDirectories.Add("...................");

                }
   else if (checkedListBox1.GetItemCheckState(2) == CheckState.Checked)
         {......}   
   
   foreach (string selectedPath in fileDirectories)
            {
                string[] level1 = Directory.GetFiles(selectedPath, DatesBetween(earliestDate, latestDate) + ".csv", SearchOption.AllDirectories);
                foreach (string level2 in level1)
                {
                    //Construct.pathList.Add(level2);

....................................................

Explanation / Answer

List<DateTime> DatesBetween(DateTime earliestDate, DateTime latestDate)
{
List<DateTime> allDates = new List<DateTime>();
for (DateTime date = earliestDate; date <= latestDate; date = date.AddDays(1))
allDates.Add(date);
return allDates.ToString();
}

Above section of code syntax wise looks like a separate method, so It should not be inside button2_Click method. It should be outside the method in the class.

This will rectify your first 2 errors as earliestDate & latestDate are already declared in your method as -

DateTime earliestDate = DateTime.ParseExact(earliestdate, "yyyyMMdd", CultureInfo.InvariantCulture);
DateTime latestDate = DateTime.ParseExact(latestdate, "yyyyMMdd", CultureInfo.InvariantCulture);

And return type of DatesBetween method is List<DateTime> but your are trying to return allDate.ToString() which is a string So either return type should be string or you can return allDate only.

like this -

private List<DateTime> DatesBetween(DateTime earliestDate, DateTime latestDate)
{
List<DateTime> allDates = new List<DateTime>();
for (DateTime date = earliestDate; date <= latestDate; date = date.AddDays(1))
allDates.Add(date);


return allDates;
}

and after having the list of dates you can loop through date and then convert each date to string before writing it in your file.