6
Answers

Serialization, Deserialization Exception

Jessica Green

Jessica Green

16y
14.3k
1
I have to make this assignment:
Visit this link for the JPG of the assignment so you know what exactly I'm supposed to do..

http://i25.tinypic.com/21owwg5.jpg

I've done much of the program but I keep getting an unhandled exception when i run it.

Here's the code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace HomeWork_2
{
    public abstract class Person
    {
        public abstract string Name { set; get; }
        public abstract string PhoneNumber { set; get; }
        public virtual void PrintInfo() { ;}
    }

    class PersonInfo : Person
    {
        private string personName;
        private string phoneNumber;

        public PersonInfo() { }

        public override string Name
        {
            set
            {
                personName = value;
            }
            get
            {
                return personName;
            }
        }

        public override string PhoneNumber
        {
            set
            {
                phoneNumber = value;
            }
            get
            {
                return phoneNumber;
            }
        }

        public override void PrintInfo()
        {
            Console.WriteLine("Name: {0} \nPhoneNumber: {1}", personName, PhoneNumber);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            bool quit = false;
            PersonInfo empInfo = new PersonInfo();
            PersonInfo[] info = new PersonInfo[10];

            try
            {
                using (Stream input = File.OpenRead("membership.dat"))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    info = (PersonInfo[])bf.Deserialize(input);
                }
            }
            catch (FileNotFoundException)
            {
            }

            while (!quit)
            {
                Console.Write("   Main Menu\n\t(1) Add \n\t(2) Search \n\t(3) Print All \n\t(4) Exit\n\t-> ");
                int option = Int32.Parse(Console.ReadLine());
               
                switch (option)
                {
                    case 1:
                        Console.Write("\nEnter Employee name: ");
                        empInfo.Name = Console.ReadLine();

                        Console.Write("\nEnter the employee's phone #: ");
                        empInfo.PhoneNumber = Console.ReadLine();

                        break;

                    case 2:
                        Console.WriteLine("Enter the name of the employee whose record you want to find\n-> ");
                        empInfo.Name = Console.ReadLine();

                        if (empInfo.Equals(empInfo))
                        {
                            Console.WriteLine("Record Found");
                        }
                        else
                            Console.WriteLine("Record not found...");
                        break;

                    case 3:
                        break;

                    case 4:
                        using (Stream output = File.Create("membership.dat"))
                        {
                            BinaryFormatter formatter = new BinaryFormatter();
                            formatter.Serialize(output, empInfo);
                        }
                        quit = true;
                        break;
                }

            }
        }
    }
}

Answers (6)