0

I have a project where to I have to load various appointments into a Calendar Application. When I implement this load method I am supposed to be able to write my data in a text file in the following format:

10/04/2015 '\t' 10:40 '\t' 30 '\t' test '\t' test.

'\t' = tab

The solution builds successfully, however when I start without debugging, the application opens in the background and doesn't show on screen. I then have to go to task-manager and end the process.

The application form is not coming into view. My application works fine without the use of this load method, however I need to be able to pre-populate my calendar aswell as manually enter appointments.

   StreamReader fileReader;
   string line;
   DateTime nStart;
   DateTime nDate;
   int nLength;
   string nSubject;
   string nLocation;
   bool result;



public bool Load()
        {
            fileReader = new StreamReader("D:\\All Downloads\\CalendarApp\\Apps.txt");
        line = fileReader.ReadLine();
        while (line != null)
        {
            string s = line;
            string[] split = s.Split('\t');                
            nDate = Convert.ToDateTime(split[0]);
            nStart = Convert.ToDateTime(split[1]);
            nLength = Convert.ToInt32(split[2]);
            nSubject = split[3];
            nLocation = split[4];
            IAppointment temp = new Appointment(nSubject, nLocation, nStart, nLength, nDate);
            listAppointments.Add(temp);
        }

        fileReader.Close();


        if (listAppointments == null)
        {
            result = false;
        }
        else
        {
            result = true;
        }
        return result;
Pradnya Bolli
  • 1,915
  • 1
  • 19
  • 37
Harvey993
  • 41
  • 1
  • 1
  • 4
  • 1
    Your `fileReader.ReadLine()` call must be included into `while` clause. It's a good practices to wrap up the StreamReader construction with `using` statement. And also, you should declare your variables inside your method when only will be used into this. – HuorSwords Apr 10 '15 at 09:50
  • 1
    You should really [encapsulate that StreamReader in a using](http://stackoverflow.com/questions/4136490/do-i-need-to-explicitly-close-the-streamreader-in-c-sharp-when-using-it-to-load). – BCdotWEB Apr 10 '15 at 09:52
  • Learn to debug. Hit the pause button in Visual Studio and see where it is. Then step through your code to see why it stays in that loop. – CodeCaster Apr 10 '15 at 09:54

1 Answers1

1

What happens when you start while debugging?

You have an infinite loop.

Try adding another call to line = fileReader.ReadLine(); at the bottom of your while loop.

Or change your loop to look like this:

while ((line = fileReader.ReadLine()) != null)
{
    ...
}

Also, these last 9 lines:

if (listAppointments == null)
{
    result = false;
}
else
{
    result = true;
}
return result;

could be written as one like this:

return listAppointments != null;

However, you haven't shown where listAppointments is declared or initialized. If it really is null, then you'll get an error when calling listAppointments.Add within your loop.

You probably want this instead:

return listAppointments.Count > 0;

Just looked at your code again. If you are only expecting one line of text, then you don't need the while loop at all. If there is more than one line, then you'll only get the values out of the last line and if there's an empty line at the end, then that would definitely throw an error in the code you have.

The whole method should look more like this (with variables any variables defined in the method not listed elsewhere):

public bool Load()
{
    using (var fileReader = new StreamReader(@"D:\All Downloads\CalendarApp\Apps.txt"))
    {
        var line = fileReader.ReadLine();
        if (line != null)
        {
            string s = line;
            string[] split = s.Split('\t');                
            nDate = Convert.ToDateTime(split[0]);
            nStart = Convert.ToDateTime(split[1]);
            nLength = Convert.ToInt32(split[2]);
            nSubject = split[3];
            nLocation = split[4];
            listAppointments.Add(new Appointment(nSubject, nLocation, nStart, nLength, nDate));
            return true;
        }
    }
    return false;
}

If that still gives an error when trying to convert to DateTime, then search for the valid way of parsing DateTime values.

CoderDennis
  • 13,642
  • 9
  • 69
  • 105
  • Hi Dennis, Thanks alot that worked perfectly, and my application now launches. I moved the 'line = fileReader.ReadLine();' to inside my while loop and now it opens the appointment, without the prepopulated data, and when I go to create a new appointment, it kicks out an exception saying "String was not recognised as a valid DateTime. – Harvey993 Apr 10 '15 at 10:02
  • IList listAppointments = new List(); was declared like this – Harvey993 Apr 10 '15 at 10:24
  • Thanks for your response. I tried your version of the Load method and moved my member variables inside of that method. Now my Application opens to main screen, it doesn't initially display the appointment, until i click off today's date to another date, then move back onto today's dates. So I think that's something to do with my Clear method possibly. "string oStart = comboBox2.SelectedItem.ToString(); start = DateTime.ParseExact(oStart, "dd MM yyyy HH:mm", System.Globalization.CultureInfo.InvariantCulture);" is the format I want to use, but it's still kicking out an exception when – Harvey993 Apr 10 '15 at 10:34
  • Thanks again Dennis, you've been a big help. – Harvey993 Apr 10 '15 at 10:43
  • Glad I could help. Feel free to post a new separate question about your date time parsing issues. – CoderDennis Apr 10 '15 at 19:20