Harvey993
Harvey993

Reputation: 41

C# Application not opening due to StreamReader

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;

Upvotes: 0

Views: 119

Answers (1)

CoderDennis
CoderDennis

Reputation: 13837

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.

Upvotes: 1

Related Questions