private static void YearsLoop(StreamReader file, int numYears, ref Years[] yearArray) { //year array is passed in by reference. for (int year = 0; year < numYears; year++) { //Gets required data from file, calls MonthlyObservationsLoop //to get Month array and constucts a year object string comment = file.ReadLine(); int yearID = Convert.ToInt32(file.ReadLine()); MonthlyObservations[] monthlyObservationArray = new MonthlyObservations[12]; MonthlyObservationsLoop(file, ref monthlyObservationArray); yearArray[year] = new Years(yearID, comment, monthlyObservationArray); } }
private static void MonthlyObservationsLoop(StreamReader file, ref MonthlyObservations[] monthlyObservations) { for (int month = 0; month < 12; month++) { //skips unneeded year line if (month != 0) { file.ReadLine(); } //Gets required data from file and constucts a MonthlyObservations object. Adds to array int monthID = Convert.ToInt32(file.ReadLine()); double maxTemp = Convert.ToDouble(file.ReadLine()); double minTemp = Convert.ToDouble(file.ReadLine()); int numDaysOfFrost = Convert.ToInt32(file.ReadLine()); double mmOfRain = Convert.ToDouble(file.ReadLine()); double hoursOfSunshine = Convert.ToDouble(file.ReadLine()); monthlyObservations[month] = new MonthlyObservations(monthID, maxTemp, minTemp, numDaysOfFrost, mmOfRain, hoursOfSunshine); } }
public static Locations[] DeepCopy() //used to clone the location array to be used to preserve the current locationArray and allow the user to cancel editing { //deep copy works very similarly to reading in data form file method. Locations[] localLocationArray = new Locations[locationArray.Length]; //create empty location array for (int location = 0; location < locationArray.Length; location++) { //get all the components for a location object string locName = locationArray[location].GetLocName(); string streetName = locationArray[location].GetStreetName(); string county = locationArray[location].GetCounty(); string postCode = locationArray[location].GetPostcode(); double latitude = locationArray[location].GetLatitude(); double longitude = locationArray[location].GetLongitude(); Years[] years = new Years[locationArray[location].GetYearArray().Length]; //create year array for (int year = 0; year < locationArray[location].GetYearArray().Length; year++) { //get components for a year array string comment = locationArray[location].GetYearArray()[year].GetYearComment(); int yearID = locationArray[location].GetYearArray()[year].GetYearID(); MonthlyObservations[] monthlyObservations = new MonthlyObservations[12]; //create month onject for (int month = 0; month < 12; month++) { //get components of a moneth object int monthID = locationArray[location].GetYearArray()[year].GetMonthlyObservationArray()[month].GetMonthID(); double maxTemp = locationArray[location].GetYearArray()[year].GetMonthlyObservationArray()[month].GetMaxTemp(); double minTemp = locationArray[location].GetYearArray()[year].GetMonthlyObservationArray()[month].GetMinTemp(); int frost = locationArray[location].GetYearArray()[year].GetMonthlyObservationArray()[month].GetNumDaysOfFrost(); double mmRain = locationArray[location].GetYearArray()[year].GetMonthlyObservationArray()[month].GetMmOfRain(); double sunshine = locationArray[location].GetYearArray()[year].GetMonthlyObservationArray()[month].GetSunshine(); monthlyObservations[month] = new MonthlyObservations(monthID, maxTemp, minTemp, frost, mmRain, sunshine); //create month object } years[year] = new Years(yearID, comment, monthlyObservations); //create year object } localLocationArray[location] = new Locations(locName, streetName, county, postCode, latitude, longitude, years); //create location object } return(localLocationArray); }