public static Location[] readFile(string fileLocation) { try { using (StreamReader data = new StreamReader(fileLocation)) { string locationName; string locationStreet; string locationCounty; string locationPostCode; float locationLatitude; float locationLongtitude; string yearDescription; int yearId; int monthId; float monthMaxTemp; float monthMinTemp; int monthDaysOfAirFrost; float monthMilsOfRainfall; float monthHoursOfSunshine; int numberOfLocations; int numberOfYears; Location thisLocation; Year thisYear; Month thisMonth; Location[] parsedLocations; Year[] parsedYears; Month[] parsedMonths; numberOfLocations = Convert.ToInt32(data.ReadLine()); parsedLocations = new Location[numberOfLocations]; for (int i = 0; i < numberOfLocations; i++) // for each location in the file { locationName = data.ReadLine(); locationStreet = data.ReadLine(); locationCounty = data.ReadLine(); locationPostCode = data.ReadLine(); locationLatitude = Convert.ToSingle(data.ReadLine()); locationLongtitude = Convert.ToSingle(data.ReadLine()); numberOfYears = Convert.ToInt32(data.ReadLine()); parsedYears = new Year[numberOfYears]; // each iteration gets a fresh array with a length equal to the number of years in this location. for (int j = 0; j < numberOfYears; j++) // for each year in this location { parsedMonths = new Month[12]; // each iteration gets a fresh array yearDescription = data.ReadLine(); yearId = Convert.ToInt32(data.ReadLine()); for (int k = 0; k < 12; k++) // for each month in this year { monthId = Convert.ToInt32(data.ReadLine()); monthMaxTemp = Convert.ToSingle(data.ReadLine()); monthMinTemp = Convert.ToSingle(data.ReadLine()); monthDaysOfAirFrost = Convert.ToInt32(data.ReadLine()); monthMilsOfRainfall = Convert.ToSingle(data.ReadLine()); monthHoursOfSunshine = Convert.ToSingle(data.ReadLine()); thisMonth = new Month(monthId, monthMaxTemp, monthMinTemp, monthDaysOfAirFrost, monthMilsOfRainfall, monthHoursOfSunshine); parsedMonths[k] = thisMonth; } thisYear = new Year(yearId, yearDescription, parsedMonths); parsedYears[j] = thisYear; } thisLocation = new Location(locationName, locationStreet, locationCounty, locationPostCode, locationLatitude, locationLongtitude, parsedYears); parsedLocations[i] = thisLocation; } return(parsedLocations); } } catch (Exception e) { Console.WriteLine("An error ocurred while executing the data import: {0}", e.Message); Console.WriteLine("Additional details: \n {0}", e); return(new Location[0]); //Null array return indicates failure state. } }
} // Creates a copy of the previous form (year display) and sets it to the same location as this form, with the same information as this form has. private void ButtonSave_Click(object sender, EventArgs e) { try { Year savedYear = new Year(); savedYear.handleDescription = textBoxDescription.Text; savedYear.handleDate = int.Parse(textBoxDate.Text); Month[] savedMonths = new Month[12]; #region reading months TextBox[] january = new TextBox[5] { textBoxJMX, textBoxJMI, textBoxJAF, textBoxJRF, textBoxJS }; savedMonths[0] = ReadTextBoxesToMonth(january, 1); TextBox[] february = new TextBox[5] { textBoxFMX, textBoxFMI, textBoxFAF, textBoxFRF, textBoxFS }; savedMonths[1] = ReadTextBoxesToMonth(february, 2); TextBox[] march = new TextBox[5] { textBoxMMX, textBoxMMI, textBoxMAF, textBoxMRF, textBoxMS }; savedMonths[2] = ReadTextBoxesToMonth(march, 3); TextBox[] april = new TextBox[5] { textBoxAMX, textBoxAMI, textBoxAAF, textBoxARF, textBoxAS }; savedMonths[3] = ReadTextBoxesToMonth(april, 4); TextBox[] may = new TextBox[5] { textBoxMAMX, textBoxMAMI, textBoxMAAF, textBoxMARF, textBoxMAS }; savedMonths[4] = ReadTextBoxesToMonth(may, 5); TextBox[] june = new TextBox[5] { textBoxJUNMX, textBoxJUNMI, textBoxJUNAF, textBoxJUNRF, textBoxJUNS }; savedMonths[5] = ReadTextBoxesToMonth(june, 6); TextBox[] july = new TextBox[5] { textBoxJULMX, textBoxJULMI, textBoxJULAF, textBoxJULRF, textBoxJULS }; savedMonths[6] = ReadTextBoxesToMonth(july, 7); TextBox[] august = new TextBox[5] { textBoxAUMX, textBoxAUMI, textBoxAUAF, textBoxAURF, textBoxAUS }; savedMonths[7] = ReadTextBoxesToMonth(august, 8); TextBox[] september = new TextBox[5] { textBoxSMX, textBoxSMI, textBoxSAF, textBoxSRF, textBoxSS }; savedMonths[8] = ReadTextBoxesToMonth(september, 9); TextBox[] october = new TextBox[5] { textBoxOMI, textBoxOMI, textBoxOAF, textBoxORF, textBoxOS }; savedMonths[9] = ReadTextBoxesToMonth(october, 10); TextBox[] november = new TextBox[5] { textBoxNMX, textBoxNMI, textBoxNAF, textBoxNRF, textBoxNS }; savedMonths[10] = ReadTextBoxesToMonth(november, 11); TextBox[] december = new TextBox[5] { textBoxDMX, textBoxDMI, textBoxDAF, textBoxDRF, textBoxDS }; savedMonths[11] = ReadTextBoxesToMonth(december, 12); #endregion // I'm not 100% happy with this; I'd have prefered to declare the textboxes in an array rather than put them in one here. However, to do that I'd have to touch autogenerated code. // Visual studio won't let me do that; it overwrites my changes at the slightest provocation. So, since I'm not using a gridView for the sake of UX, I think this is my best option. savedYear.handleMonths = savedMonths; locations[location].AddYear(savedYear); Filehandler.SaveFile(locations); string message = "The year has been saved."; string title = "Success!"; MessageBox.Show(message, title); } catch (Exception badInputError) { Console.WriteLine("An error ocurred while rading the form: {0}", badInputError.Message); Console.WriteLine("Additional details: \n {0}", badInputError); Console.WriteLine("It's probably because you entered something wrong, user."); string message = "The year has not been saved due to an invalid input."; string title = "Error."; MessageBox.Show(message, title); } } // Saves the data entered into this form into a new year, using the Location class's AddYear function.