// LoadFromFile loads the entire school from file, given as a parameter // returns -1 if unable to find file, in which case School is reset public int LoadFromFile(String fileName) { School tempSchool; if (!File.Exists(fileName)) { // if the file doesn't exist return error code -1 return(-1); } // Open the file containing the data that you want to deserialize. FileStream fs = new FileStream(fileName, FileMode.Open); try { Console.WriteLine("Deserializing vector"); BinaryFormatter formatter = new BinaryFormatter(); tempSchool = (School)formatter.Deserialize(fs); fs.Close(); theSubjects = tempSchool.theSubjects; theTeachers = tempSchool.theTeachers; theStudents = tempSchool.theStudents; return(0); } catch (Exception e) { // we go here in the case of an exception Console.WriteLine("Failed to deserialize. Reason: " + e.Message); throw; } finally { fs.Close(); } }
// constructor allowing maximum sizes to be set for subjects, teachers and students. public School(int maxSubjects, int maxTeachers, int maxStudents) { theSubjects = new Subjects(maxSubjects); theTeachers = new Teachers(maxTeachers); theStudents = new SchoolRecords.Students(maxStudents); }