private static List <Person> LoadFromBinaryFile() { if (File.Exists(BinaryFile)) { BinaryReader binaryreader = new BinaryReader(new FileStream(BinaryFile, FileMode.Open)); int limit = binaryreader.ReadInt32(); List <Person> newList = new List <Person>(); for (int i = 0; i < limit; i++) { string name = binaryreader.ReadString(); int year = binaryreader.ReadInt32(); int month = binaryreader.ReadInt32(); int day = binaryreader.ReadInt32(); DateTime bday = new DateTime(year, month, day); string gender = binaryreader.ReadString(); string eyecolor = binaryreader.ReadString(); string haircolor = binaryreader.ReadString(); float hairlength = binaryreader.ReadSingle();//Reads in float HairData hair = new HairData(haircolor, hairlength); Person newp = new Person(name, bday, gender, eyecolor, hair); newList.Add(newp); } binaryreader.Close(); return(newList); } return(null); }
private static void SaveToBinaryFile(List <Person> plist) { if (plist.Count == 0) { return; } //BinaryFile BinaryWriter binarywriter = new BinaryWriter(new FileStream(BinaryFile, FileMode.Create)); binarywriter.Write(plist.Count); //int foreach (Person pitem in plist) { binarywriter.Write(pitem.Name);//string // DateTime dt = pitem.Birthday; binarywriter.Write(dt.Year); //int binarywriter.Write(dt.Month); //int binarywriter.Write(dt.Day); //int binarywriter.Write(pitem.Gender); //string binarywriter.Write(pitem.EyeColor); //string HairData hd = pitem.Hair; binarywriter.Write(hd.Color); //string binarywriter.Write(hd.Lenght); //float } binarywriter.Flush(); binarywriter.Close(); }
public Person(string name, DateTime birthday, string gender, string eyecolor, HairData hdata) { Name = name; Birthday = birthday; Gender = gender; EyeColor = eyecolor; privHair = hdata; }
public void SetHair(HairData h) { privHair = h; }