Home

Introduction to Serialization

 

Introduction

Consider the following program:

using System;
using System.IO;

class Program
{
    static int Main(string[] args)
    {
        string Make  = "Ford";
        string Model = "Escort";
        uint Year    = 1998;
        byte Color   = 1;

        FileStream stmCar   = new FileStream("Car1.car",
                                             FileMode.Create);
        BinaryWriter bnwCar = new BinaryWriter(stmCar);

        try
        {
            bnwCar.Write(Make);
            bnwCar.Write(Model);
            bnwCar.Write(Year);
            bnwCar.Write(Color);
        }
        finally
        {
            bnwCar.Close();
            stmCar.Close();
        }

        return 0;
    }
}

This is an example of the techniques we have used in previous lessons to save individual data of primitive types:

Saving the variables in a method

The values can be retrieved with the following code:

using System;
using System.IO;

class Program
{
    static int Main(string[] args)
    {
        string Make  = "Ford";
        string Model = "Escort";
        uint Year    = 1998;
        byte Color   = 1;

        FileStream stmCar   = new FileStream("Car1.car",
                                             FileMode.Open);
        BinaryReader bnrCar = new BinaryReader(stmCar);

        try
        {
            Console.WriteLine("Make:  {0}", bnrCar.ReadString());
            Console.WriteLine("Model: {0}", bnrCar.ReadString());
            Console.WriteLine("Year:  {0}", bnrCar.ReadUInt32());
            Console.Write("Color: ");
            byte  clr = bnrCar.ReadByte();
            switch (clr)
            {
                case 1:
                    Console.WriteLine("Black");
                    break;
                case 2:
                    Console.WriteLine("Gray");
                    break;
                case 3:
                    Console.WriteLine("White");
                    break;
                case 4:
                    Console.WriteLine("Red");
                    break;
                case 5:
                    Console.WriteLine("Blue");
                    break;
            }
        }
        finally
        {
            bnrCar.Close();
            stmCar.Close();
        }

        return 0;
    }
}

This would produce:

Make:  Ford
Model: Escort
Year:  1998
Color: Black
Press any key to continue . . .

In the same way, we learned to save the individual fields of a class:

Saving the individual parts of an object

Here is an example:

using System;
using System.IO;

public class Car
{
    public string Make  = "Toyota";
    public string Model = "Corolla";
    public uint   Year  = 2002;
    public byte   Color = 2;
}

class Program
{
    static int Main(string[] args)
    {
        Car vehicle = new Car();
        
        FileStream stmCar   = File.Create("Car2.car");
        BinaryWriter bnwCar = new BinaryWriter(stmCar);

        try
        {
            bnwCar.Write(vehicle.Model);
            bnwCar.Write(vehicle.Year);
            bnwCar.Write(vehicle.Color);
        }
        finally
        {
            bnwCar.Close();
            stmCar.Close();
        }

        return 0;
    }
}

When it comes to a class, the problem with saving individual fields is that you could forget to save one of the fields. For example, considering a Car class, if you don't save the Make information of a Car object and retrieve or open the saved object on another computer, the receiving user would miss some information and the car cannot be completely identifiable. An alternative is to save the whole Car object.

Object serialization consists of saving a whole object as one instead of its individual fields:

Serialization

In other words, a variable declared from a class can be saved to a stream and then the saved object can be retrieved later or on another computer. The .NET Framework supports two types of object serialization: binary and SOAP.

Practical Learning: Introducing Serialization

  1. Start Microsoft Visual C# and create a Console Application named GeorgetownCleaningServices6
  2. To save the project, on the Standard toolbar, click the Save All button
  3. Accept all the defaults and click Save
  4. To create a new class, in the Solution Explorer, right-click GeorgetownCleaningServices6 -> Add -> Class...
  5. Set the Name to BusinessManagement and press Enter
  6. Change the file as follows:
     
    using System;
    using System.IO;
    
    namespace GeorgetownCleaningServices6
    {
        public static class BusinessManagement
        {
            public static void HireEmployee()
            {
                FileStream fsEmployee = null;
                BinaryWriter bwEmployee = null;
    
                // Ask the clerk to enter an employee number
                // using the format 00-000
                Console.Write("Enter Employee Number (00-000): ");
                string emplNumber = Console.ReadLine();
    
                string strPath = @"C:\Georgetown Cleaning Services\Employees\" +
                                 @"\" + emplNumber + ".gce";
    
                if (File.Exists(strPath))
                {
                    Console.Write("\nEither the employee has already been hired, ");
        Console.WriteLine("or there is already another employee with that number.");
                    return;
                }
                else // If no employee with that number was found, create it
                {
                    try
                    {
                 // If there is not yet a directory named Employees, then create it
        Directory.CreateDirectory(@"C:\Georgetown Cleaning Services\Employees");
                    }
                    catch (DirectoryNotFoundException)
                    {
                        Console.WriteLine("The folder could not be created");
                    }
    
                    try
                    {
                        fsEmployee = File.Create(strPath);
                        bwEmployee = new BinaryWriter(fsEmployee);
                        Console.Write("Enter Employee First Name: ");
                        string emplFName = Console.ReadLine();
                        Console.Write("Enter Employee Last Name:  ");
                        string emplLName = Console.ReadLine();
                        Console.Write("Enter Hourly Salary: ");
                        double emplSalary = double.Parse(Console.ReadLine());
    
                        // The minimum salary in this company is 7.50
                        if (emplSalary < 7.50D)
                            emplSalary = 7.50D;
    
                        bwEmployee.Write(emplNumber);
                        bwEmployee.Write(emplFName);
                        bwEmployee.Write(emplLName);
                        bwEmployee.Write(emplSalary);
                    }
                    finally
                    {
                        bwEmployee.Close();
                        fsEmployee.Close();
                    }
                }
                
                Console.WriteLine();
            }       
        }
    }
  7. Access the Program.cs file and change it as follows:
     
    using System;
    
    namespace GeorgetownCleaningServices6
    {
        public static class Program
        {
            static int Main(string[] args)
            {
                char answer = '0';
    
                do
                {
                    try
                    {
                        Console.WriteLine("What do you want to do?");
                        Console.WriteLine("0. Quit");
                        Console.WriteLine("1. Hire a new employee");
                        Console.WriteLine("2. Process a payroll");
                        Console.Write("Your Choice: ");
                        answer = char.Parse(Console.ReadLine());
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("Invalid Answer!");
                    }
    
                    switch (answer)
                    {
                        case '1':
                            BusinessManagement.HireEmployee();
                            break;
    
                        case '2':
                            break;
    
                        default:
                            break;
                    }
    
                } while (answer == '1' || answer == '2');
    
                Console.WriteLine();
                return 0;
            }
        }
    }
  8. Execute the application and test it. Here is an example:
     
    What do you want to do?
    0. Quit
    1. Hire a new employee
    2. Process a payroll
    Your Choice: 1
    Enter Employee Number (00-000): 86-025
    Enter Employee First Name: Anne
    Enter Employee Last Name: Harang
    Enter Hourly Salary: 6.75
    
    What do you want to do?
    0. Quit
    1. Hire a new employee
    2. Process a payroll
    Your Choice: 1
    Enter Employee Number (00-000): 42-713
    Enter Employee First Name: Peter
    Enter Employee Last Name: Lansome
    Enter Hourly Salary: 12.45
    
    What do you want to do?
    0. Quit
    1. Hire a new employee
    2. Process a payroll
    Your Choice: 1
    Enter Employee Number (00-000): 29-368
    Enter Employee First Name: Gertrude
    Enter Employee Last Name: Monay
    Enter Hourly Salary: 10.85
    
    What do you want to do?
    0. Quit
    1. Hire a new employee
    2. Process a payroll
    Your Choice: 0
    
    Press any key to continue . . .
  9. Close the DOS window

Serialization

Binary serialization works by processing an object rather than streaming its individual member variables. This means that, to use it, you define an object and initialize it, or "fill" it, with the necessary values and any information you judge necessary. This creates a "state" of the object. It is this state that you prepare to serialize. When you save the object, it is converted into a stream.

To perform binary serialization, there are a few steps you must follow. When creating the class whose objects would be serialized, start it with the [Serializable] attribute. Here is an example:

[Serializable]
public class Car
{
    public string Make;
    public string Model;
    public uint   Year;
    public byte   Color;
}

Before serializing an object, you should reference the System.Runtime.Serialization.Formatters.Binary namespace. The class responsible for binary serialization is called BinaryFormatter. This class is equipped with two constructors. The default constructor is used to simply create an object. After declaring the variable, to actually serialize an object, call the Serialize() method of the BinaryFormatter class. The method is overloaded with two versions. One of the versions of this method uses the following syntax:

public void Serialize(Stream serializationStream, object graph);

The first argument to this method must be an object of a Stream-based class. In the previous lessons, we saw how to create Stream objects (for example using the FileStream class).

The second argument must be the object to serialize. This means that, before calling this method, you should have built the object.

Here is an example:

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

[Serializable]
public class Car
{
    public string Make;
    public string Model;
    public uint   Year;
    public byte   Color;
}

class Program
{
    static int Main(string[] args)
    {
        Car vehicle = new Car();

        vehicle.Make  = "Lexus";
        vehicle.Model = "LS";
        vehicle.Year  = 2007;
        vehicle.Color = 4;

        FileStream stmCar   = new FileStream("Car3.car",
                                             FileMode.Create);
        BinaryFormatter bfmCar = new BinaryFormatter();
        
        bfmCar.Serialize(stmCar, vehicle);

        return 0;
    }
}

Practical Learning: Serializing an Object

  1. To create a new class, in the Class View, right-click GeorgetownCleaningServices6 -> Add -> Class...
  2. Set the Name to PayrollInformation and click OK
  3. Change the file as follows:
     
    using System;
    
    namespace GeorgetownCleaningServices6a
    {
        [Serializable]
        public class PayrollInformation
        {
            private string number;
            private string fname;
            private string lname;
            private double salary;
            private DateTime start;
    
            public string EmployeeNumber
            {
                get { return this.number; }
                set { this.number = value; }
            }
    
            public string FirstName
            {
                get { return this.fname; }
                set { this.fname = value; }
            }
    
            public string LastName
            {
                get { return this.lname; }
                set { this.lname = value; }
            }
    
            public double HourlySalary
            {
                get { return this.salary; }
                set { this.salary = value; }
            }
    
            public DateTime StartPeriod
            {
                get { return this.start; }
                set { this.start = value; }
            }
    
            public DateTime EndPeriod
            {
                get { return start.AddDays(13D); }
            }
    
            public double Week1Monday;
            public double Week1Tuesday;
            public double Week1Wednesday;
            public double Week1Thursday;
            public double Week1Friday;
            public double Week2Monday;
            public double Week2Tuesday;
            public double Week2Wednesday;
            public double Week2Thursday;
            public double Week2Friday;
    
            public double RegularHours;
            public double OvertimeHours;
            public double RegularAmount;
            public double OvertimeAmount;
    
            public double TotalEarnings;
        }
    }
  4. Access the BusinessManagement.cs file and change it as follows:
     
    using System;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace GeorgetownCleaningServices6a
    {
        public static class BusinessManagement
        {
            public static void HireEmployee()
            {
                FileStream fsEmployee = null;
                BinaryWriter bwEmployee = null;
    
                // Ask the clerk to enter an employee number
                // using the format 00-000
                Console.Write("Enter Employee Number (00-000): ");
                string emplNumber = Console.ReadLine();
    
                string strPath = @"C:\Georgetown Cleaning " +
                                 @"Services\Employees\" +
                                 @"\" + emplNumber + ".gce";
    
                if (File.Exists(strPath))
                {
                    Console.Write("\nEither the employee has " +
                                  "already been hired, ");
                    Console.WriteLine("or there is already another " +
                                      "employee with that number.");
                    return;
                }
                // If no employee with that number 
                // was found, create it
                else 
                {
                    try
                    {
                        // If there is not yet a directory named 
                        // Employees, then create it
                        Directory.CreateDirectory(@"C:\Georgetown " +
                            @"Cleaning Services\Employees");
                    }
                    catch (DirectoryNotFoundException)
                    {
                        Console.WriteLine("The folder could " +
                                          "not be created");
                    }
    
                    try
                    {
                        fsEmployee = File.Create(strPath);
                        bwEmployee = new BinaryWriter(fsEmployee);
    
                        Console.Write("Enter Employee First Name: ");
                        string emplFName = Console.ReadLine();
                        Console.Write("Enter Employee Last Name:  ");
                        string emplLName = Console.ReadLine();
                        Console.Write("Enter Hourly Salary:       ");
                        double emplSalary =
                            double.Parse(Console.ReadLine());
    
                        // The minimum salary in this company is 7.50
                        if (emplSalary < 7.50D)
                            emplSalary = 7.50D;
    
                        bwEmployee.Write(emplNumber);
                        bwEmployee.Write(emplFName);
                        bwEmployee.Write(emplLName);
                        bwEmployee.Write(emplSalary);
                    }
                    finally
                    {
                        bwEmployee.Close();
                        fsEmployee.Close();
                    }
                }
    
                Console.WriteLine();
            }
    
            public static void CreatePayroll()
            {
                FileStream   fsPayroll = null;
                BinaryReader brPayroll = null;
                DateTime dteStartDate;
    
                PayrollInformation payroll =
                    new PayrollInformation();
    
                double monday1 = 0.00D, tuesday1 = 0.00D,
                       wednesday1 = 0.00D, thursday1 = 0.00D,
                       friday1 = 0.00D, monday2 = 0.00D,
                       tuesday2 = 0.00D, wednesday2 = 0.00D,
                       thursday2 = 0.00D, friday2 = 0.00D;
                double totalHoursWeek1 = 0.00D,
                       totalHoursWeek2 = 0.00D;
    
                double regHours1 = 0.00D, regHours2 = 0.00D,
                       ovtHours1 = 0.00, ovtHours2 = 0.00;
                double regAmount1 = 0.00D, regAmount2 = 0.00D,
                       ovtAmount1 = 0.00D, ovtAmount2 = 0.00D;
                double regularHours = 0.00D, overtimeHours = 0.00D;
                double regularAmount = 0.00D,
                       overtimeAmount = 0.00D,
                       totalEarnings = 0.00D;
    
                Console.Write("Enter Employee Number: ");
                string emplNumber = Console.ReadLine();
    
                string strEmployeePath = @"C:\Georgetown " +
                                         @"Cleaning Services\Employees\" +
                                         @"\" + emplNumber + ".gce";
    
                if (!File.Exists(strEmployeePath))
                {
                    Console.WriteLine("There is no employee with " +
                                      "that number in our records");
                    return;
                }
                // If an employee with that number was found,
                // continue with the payroll
                else {
                    try
                    {
                        fsPayroll = new FileStream(strEmployeePath,
                                                   FileMode.Open,
                                                   FileAccess.Read);
                        brPayroll = new BinaryReader(fsPayroll);
    
                        payroll.EmployeeNumber = brPayroll.ReadString();
                        payroll.FirstName = brPayroll.ReadString();
                        payroll.LastName = brPayroll.ReadString();
                        payroll.HourlySalary = brPayroll.ReadDouble();
    
                        Console.WriteLine("\n------------------------" +
                                          "------------------------");
                        Console.WriteLine("Employee #:    {0}",
                                          payroll.EmployeeNumber);
                        Console.WriteLine("Full Name:     {0}, {1}",
                                          payroll.FirstName,
                                          payroll.LastName);
                        Console.WriteLine("Hourly Salary: {0:C}",
                                          payroll.HourlySalary);
                        Console.WriteLine("-------------------------" +
                                          "-----------------------\n");
                    }
                    finally
                    {
                        brPayroll.Close();
                        fsPayroll.Close();
                    }
    
                    try
                    {
                        do
                        {
                            Console.Write("Enter Payroll Start " +
                                          "Date (mm/dd/yyyy): ");
                            dteStartDate =
                                DateTime.Parse(Console.ReadLine());
                            if (dteStartDate.DayOfWeek !=
                                DayOfWeek.Sunday)
                            {
                                Console.WriteLine("Invalid Date Entry");
                                Console.WriteLine("Payrolls start " +
                                                  "on a Sunday");
                            }
                        } while (dteStartDate.DayOfWeek !=
                              DayOfWeek.Sunday);
    
                        payroll.StartPeriod = dteStartDate;
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("Invalid Date Entry");
                    }
                }
    
                // Retrieve the value of each day worked
                Console.WriteLine("\nEnter the time worked " +
                                  "for each day (0.00)");
                Console.WriteLine("=-= Week 1 =-=");
                try
                {
                    Console.Write("{0}: ",
                        payroll.StartPeriod.AddDays(1).ToString("D"));
                    monday1 = double.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("You typed an invalid value");
                }
                try
                {
                    Console.Write("{0}: ",
                        payroll.StartPeriod.AddDays(2).ToString("D"));
                    tuesday1 = double.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("You typed an invalid value");
                }
                try
                {
                    Console.Write("{0}: ",
                        payroll.StartPeriod.AddDays(3).ToString("D"));
                    wednesday1 = double.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("You typed an invalid value");
                }
                try
                {
                    Console.Write("{0}: ",
                        payroll.StartPeriod.AddDays(4).ToString("D"));
                    thursday1 = double.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("You typed an invalid value");
                }
                try
                {
                    Console.Write("{0}: ",
                        payroll.StartPeriod.AddDays(5).ToString("D"));
                    friday1 = double.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("You typed an invalid value");
                }
    
                Console.WriteLine("=-= Week 2 =-=");
                try
                {
                    Console.Write("{0}: ",
                        payroll.StartPeriod.AddDays(8).ToString("D"));
                    monday2 = double.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("You typed an invalid value");
                }
                try
                {
                    Console.Write("{0}: ",
                        payroll.StartPeriod.AddDays(9).ToString("D"));
                    tuesday2 = double.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("You typed an invalid value");
                }
                try
                {
                    Console.Write("{0}: ",
                        payroll.StartPeriod.AddDays(10).ToString("D"));
                    wednesday2 = double.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("You typed an invalid value");
                }
                try
                {
                    Console.Write("{0}: ",
                        payroll.StartPeriod.AddDays(11).ToString("D"));
                    thursday2 = double.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("You typed an invalid value");
                }
                try
                {
                    Console.Write("{0}: ",
                        payroll.StartPeriod.AddDays(12).ToString("D"));
                    friday2 = double.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("You typed an invalid value");
                }
    
                // Calculate the total number of hours for each week
                totalHoursWeek1 = monday1 + tuesday1 + wednesday1 +
                                  thursday1 + friday1;
                totalHoursWeek2 = monday2 + tuesday2 + wednesday2 +
                                  thursday2 + friday2;
    
                // The overtime is paid time and half
                double ovtSalary = payroll.HourlySalary * 1.5D;
    
                // If the employee worked under 40 hours,
                // there is no overtime
                if (totalHoursWeek1 < 40)
                {
                    regHours1 = totalHoursWeek1;
                    regAmount1 = payroll.HourlySalary * regHours1;
                    ovtHours1 = 0.00D;
                    ovtAmount1 = 0.00D;
                } // If the employee worked over 40 hours,
                  // calculate the overtime
                else if (totalHoursWeek1 >= 40)
                {
                    regHours1 = 40;
                    regAmount1 = payroll.HourlySalary * 40;
                    ovtHours1 = totalHoursWeek1 - 40;
                    ovtAmount1 = ovtHours1 * ovtSalary;
                }
    
                if (totalHoursWeek2 < 40)
                {
                    regHours2 = totalHoursWeek2;
                    regAmount2 = payroll.HourlySalary * regHours2;
                    ovtHours2 = 0.00D;
                    ovtAmount2 = 0.00D;
                }
                else if (totalHoursWeek2 >= 40)
                {
                    regHours2 = 40;
                    regAmount2 = payroll.HourlySalary * 40;
                    ovtHours2 = totalHoursWeek2 - 40;
                    ovtAmount2 = ovtHours2 * ovtSalary;
                }
    
                regularHours = regHours1 + regHours2;
                overtimeHours = ovtHours1 + ovtHours2;
                regularAmount = regAmount1 + regAmount2;
                overtimeAmount = ovtAmount1 + ovtAmount2;
                totalEarnings = regularAmount + overtimeAmount;
    
                payroll.Week1Monday = monday1;
                payroll.Week1Tuesday = tuesday1;
                payroll.Week1Wednesday = wednesday1;
                payroll.Week1Thursday = thursday1;
                payroll.Week1Friday = friday1;
    
                payroll.Week2Monday = monday2;
                payroll.Week2Tuesday = tuesday2;
                payroll.Week2Wednesday = wednesday2;
                payroll.Week2Thursday = thursday2;
                payroll.Week2Friday = friday2;
    
                payroll.RegularHours = regularHours;
                payroll.OvertimeHours = overtimeHours;
                payroll.RegularAmount = regularAmount;
                payroll.OvertimeAmount = overtimeAmount;
                payroll.TotalEarnings = totalEarnings;
    
                ShowPayroll(payroll);
    
                Console.Write("Do you want to save " +
                              "the payroll (y/n): ");
                string strAnswer = Console.ReadLine();
    
                if (strAnswer.ToUpper() == "Y")
                    SavePayroll(payroll);
            }
    
            public static void SavePayroll(PayrollInformation pay)
            {
                // We will need this value to create the
                // name of the payroll file
                string strMonth = "0", strDay = "0", strYear = "0";
    
                // We want the month and day to include 0 if necessary
                strMonth = pay.StartPeriod.Month.ToString();
                if (pay.StartPeriod.Month < 10)
                    strMonth = "0" + pay.StartPeriod.Month.ToString();
                strDay = pay.StartPeriod.Day.ToString();
                if (pay.StartPeriod.Day < 10)
                    strDay = "0" + pay.StartPeriod.Day.ToString();
                strYear = pay.StartPeriod.Year.ToString();
    
                string strPayrollFilename = @"C:\Georgetown " +
                                            @"Cleaning Services\Payrolls\" +
                                            @"\" + pay.LastName[0] +
                                            pay.FirstName[0] +
                                            strMonth + strDay +
                                            strYear + ".epr";
                try
                {
                    // If there is not yet a directory for the 
                    // payrolls, then create it
                    Directory.CreateDirectory(@"C:\Georgetown Cleaning " +
                                              @"Services\Payrolls");
                }
                catch (DirectoryNotFoundException)
                {
                    Console.WriteLine("The employee payroll file " +
                                      "could not be created");
                }
    
                if (File.Exists(strPayrollFilename))
                {
                    Console.WriteLine("The employee's payroll " +
                                      "for that period exists already");
                }
    
                FileStream fsEmployeePayroll =
                                     new FileStream(strPayrollFilename,
                                     FileMode.Create);
                BinaryFormatter bfEmployeePayroll =
                    new BinaryFormatter();
    
                bfEmployeePayroll.Serialize(fsEmployeePayroll, pay);
                fsEmployeePayroll.Close();
            }
    
            public static void ViewPayroll()
            {
                
            }
    
            public static void ShowPayroll(PayrollInformation payed)
            {
                Console.WriteLine("\n=============================" +
                                  "===================");
                Console.WriteLine("=$= Payroll summary =$=");
                Console.WriteLine("-------------------------------" +
                                  "-----------------");
                Console.WriteLine("Employee #:    {0}",
                                  payed.EmployeeNumber);
                Console.WriteLine("Full Name:     {0}, {1}",
                                  payed.FirstName, payed.LastName);
                Console.WriteLine("Hourly Salary: {0:C}",
                                  payed.HourlySalary);
                Console.WriteLine("Start Period:  {0:D}",
                                  payed.StartPeriod);
                Console.WriteLine("End Period:    {0:D}",
                                  payed.EndPeriod);
                Console.WriteLine("--------------------------------" +
                                  "----------------");
                Console.WriteLine("        Monday Tuesday Wednesday " +
                                  "Thursday Friday");
                Console.WriteLine("Week 1:  {0:F}   {1:F}     " +
                                  "{2:F}      {3:F}    {4:F}",
                                  payed.Week1Monday, payed.Week1Tuesday,
                                  payed.Week1Wednesday, payed.Week1Thursday,
                                  payed.Week1Friday);
                Console.WriteLine("Week 2:  {0:F}   {1:F}     " +
                                  "{2:F}      {3:F}    {4:F}",
                                  payed.Week2Monday, payed.Week2Tuesday,
                                  payed.Week2Wednesday, payed.Week2Thursday,
                                  payed.Week2Friday);
                Console.WriteLine("-------------------------------" +
                                  "-----------------");
                Console.WriteLine("Monetary Summary");
                Console.WriteLine("            Hours   Amount");
                Console.WriteLine("Regular:   {0,6}   {1,6}",
                                  payed.RegularHours.ToString("F"),
                                  payed.RegularAmount.ToString("F"));
                Console.WriteLine("Overtime:  {0,6}   {1,6}",
                                  payed.OvertimeHours.ToString("F"),
                                  payed.OvertimeAmount.ToString("F"));
                Console.WriteLine("-------------------------------" +
                                  "-----------------");
                Console.WriteLine("Net Pay:   {0:F}",
                                  payed.TotalEarnings);
                Console.WriteLine("===============================" +
                                  "=================\n");
            }
        }
    }
  5. Access the Program.cs file and change it as follows:
     
    using System;
    
    namespace GeorgetownCleaningServices6a
    {
        public static class Program
        {
            static int Main(string[] args)
            {
                char answer = '0';
    
                Console.WriteLine("========================" +
                                  "========================");
                Console.WriteLine("Georgetown Cleaning Services");
                Console.WriteLine("========================" +
                                  "========================");
    
                do
                {
                    try
                    {
                        Console.WriteLine("\nWhat do you want to do?");
                        Console.WriteLine("0. Quit");
                        Console.WriteLine("1. Hire a new employee");
                        Console.WriteLine("2. Process a payroll");
                        Console.WriteLine("3. View an employee's payroll");
                        Console.Write("Your Choice: ");
                        answer = char.Parse(Console.ReadLine());
                        Console.WriteLine();
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("Invalid Answer!");
                    }
    
                    switch (answer)
                    {
                        case '1':
                            BusinessManagement.HireEmployee();
                            break;
                        case '2':
                            BusinessManagement.CreatePayroll();
                            break;
                        case '3':
                            BusinessManagement.ViewPayroll();
                            break;
                        default:
                            break;
                    }
    
                } while( (answer == '1') ||
                         (answer == '2') ||
                         (answer == '3') );
    
                return 0;
            }
        }
    }
  6. Execute the application and test it. Here is an example:
     
    ================================================
    Georgetown Cleaning Services
    ================================================
    
    What do you want to do?
    0. Quit
    1. Hire a new employee
    2. Process a payroll
    3. View an employee's payroll
    Your Choice: 2
    
    Enter Employee Number: 29-368
    
    ------------------------------------------------
    Employee #:    29-368
    Full Name:     Gertrude, Monay
    Hourly Salary: $10.85
    ------------------------------------------------
    
    Enter Payroll Start Date (mm/dd/yyyy): 11/12/05
    Invalid Date Entry
    Payrolls start on a Sunday
    Enter Payroll Start Date (mm/dd/yyyy): 11/12/06
    
    Enter the time worked for each day (0.00)
    =-= Week 1 =-=
    Monday, November 13, 2006: 8.00
    Tuesday, November 14, 2006: 8.50
    Wednesday, November 15, 2006: 9.50
    Thursday, November 16, 2006: 8
    Friday, November 17, 2006: 8.50
    =-= Week 2 =-=
    Monday, November 20, 2006: 6.50
    Tuesday, November 21, 2006: 7.00
    Wednesday, November 22, 2006: 8
    Thursday, November 23, 2006: 6.00
    Friday, November 24, 2006: 7.00
    
    ================================================
    =$= Payroll summary =$=
    ------------------------------------------------
    Employee #:    29-368
    Full Name:     Gertrude, Monay
    Hourly Salary: $10.85
    Start Period:  Sunday, November 12, 2006
    End Period:    Saturday, November 25, 2006
    ------------------------------------------------
            Monday Tuesday Wednesday Thursday Friday
    Week 1:  8.00   8.50     9.50      8.00    8.50
    Week 2:  6.50   7.00     8.00      6.00    7.00
    ------------------------------------------------
    Monetary Summary
                Hours   Amount
    Regular:    74.50   808.33
    Overtime:    2.50    40.69
    ------------------------------------------------
    Net Pay:   849.01
    ================================================
    
    Do you want to save the payroll (y/n): Y
    
    What do you want to do?
    0. Quit
    1. Hire a new employee
    2. Process a payroll
    3. View an employee's payroll
    Your Choice: 0
    
    Press any key to continue . . .
  7. Close the DOS window
  8. Execute the application again and process another payroll. Here is an example:
     
    ================================================
    Georgetown Cleaning Services
    ================================================
    
    What do you want to do?
    0. Quit
    1. Hire a new employee
    2. Process a payroll
    3. View an employee's payroll
    Your Choice: 2
    
    Enter Employee Number: 86-025
    
    ------------------------------------------------
    Employee #:    86-025
    Full Name:     Anne, Harang
    Hourly Salary: $7.50
    ------------------------------------------------
    
    Enter Payroll Start Date (mm/dd/yyyy): 11/26/2006
    
    Enter the time worked for each day (0.00)
    =-= Week 1 =-=
    Monday, November 27, 2006: 8.00
    Tuesday, November 28, 2006: 6.50
    Wednesday, November 29, 2006: 8.50
    Thursday, November 30, 2006: 8.00
    Friday, December 01, 2006: 8.00
    =-= Week 2 =-=
    Monday, December 04, 2006: 9.00
    Tuesday, December 05, 2006: 8.50
    Wednesday, December 06, 2006: 8.00
    Thursday, December 07, 2006: 9.50
    Friday, December 08, 2006: 8.00
    
    ================================================
    =$= Payroll summary =$=
    ------------------------------------------------
    Employee #:    86-025
    Full Name:     Anne, Harang
    Hourly Salary: $7.50
    Start Period:  Sunday, November 26, 2006
    End Period:    Saturday, December 09, 2006
    ------------------------------------------------
            Monday Tuesday Wednesday Thursday Friday
    Week 1:  8.00   6.50     8.50      8.00    8.00
    Week 2:  9.00   8.50     8.00      9.50    8.00
    ------------------------------------------------
    Monetary Summary
                Hours   Amount
    Regular:    79.00   592.50
    Overtime:    3.00    33.75
    ------------------------------------------------
    Net Pay:   626.25
    ================================================
    
    Do you want to save the payroll (y/n): y
    
    What do you want to do?
    0. Quit
    1. Hire a new employee
    2. Process a payroll
    3. View an employee's payroll
    Your Choice: 0
    
    Press any key to continue . . .
  9. Close the DOS window

De-Serialization

As serialization is the process of storing an object to a medium, the opposite, serialization is used to retrieve an object from a stream. To support this, the BinaryFormatter class is equipped with the Deserialize() method. Like Serialize(), the Deserialize() method is overloaded with two versions. One of them uses the following syntax:

public object Deserialize(Stream serializationStream);

This method takes as argument a Stream-based object, such as a FileStream variable, that indicates where the file is located The Deserialize() method. The Deserialize() method returns an Object object. As a goal, you want the Deserialize() method to produce the type of object that was saved so you can retrieve the values that the returned object holds. Because the method returns an Object value, you must cast the returned value to the type of your class.

Once the Deserialize() method has returned the desired object, you can access its values. Here is an example:

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

[Serializable]
public class Car
{
    public string Make;
    public string Model;
    public uint Year;
    public byte Color;
}

class Program
{
    static int Main(string[] args)
    {
        FileStream stmCar   = new FileStream("Car3.car",
                                             FileMode.Open);
        BinaryFormatter bfmCar = new BinaryFormatter();
        Car vehicle = (Car)bfmCar.Deserialize(stmCar);

        Console.WriteLine("Make:  {0}", vehicle.Make);
        Console.WriteLine("Model: {0}", vehicle.Model);
        Console.WriteLine("Year:  {0}", vehicle.Year);
        byte clr = vehicle.Color;
        
        switch (clr)
        {
        case 1:
            Console.WriteLine("Black");
            break;
        case 2:
            Console.WriteLine("Gray");
            break;
        case 3:
            Console.WriteLine("White");
            break;
        case 4:
            Console.WriteLine("Red");
            break;
        case 5:
            Console.WriteLine("Blue");
            break;
        }
        
        stmCar.Close();

        return 0;
    }
}

Practical Learning: De-Serializing an Object

  1. Access the BusinessManagement.cs file and implement the ViewPayroll() method as follows:
     
    using System;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace GeorgetownCleaningServices6a
    {
        public static class BusinessManagement
        {
            public static void HireEmployee()
            {
                . . . No Change
            }
    
            public static void CreatePayroll()
            {
                . . . No Change
            }
    
            public static void SavePayroll(PayrollInformation pay)
            {
                . . . No Change
            }
    
            public static void ViewPayroll()
            {
                string strEmplNumber, strFName, strLName;
                string strMonth, strDay, strYear;
    
                // Ask the clerk to enter an employee number
                // using the format 00-000
                Console.Write("Enter Employee Number (00-000): ");
                strEmplNumber = Console.ReadLine();
    
                string strFilename = @"C:\Georgetown Cleaning " +
                                     @"Services\Employees\" +
                                     @"\" + strEmplNumber + ".gce";
    
                if (!File.Exists(strFilename))
                {
                    Console.Write("There is no employee " +
                                  "with that number.");
                }
                else
                {
                    FileStream fsEmployee = null;
                    BinaryReader brEmployee = null;
    
                    try
                    {
                        // We need to formally open the file 
                        // because we need the employees initials
                        fsEmployee = new FileStream(strFilename,
                                                    FileMode.Open,
                                                    FileAccess.Read);
                        brEmployee = new BinaryReader(fsEmployee);
    
                        // Read the file, mainly to get the 
                        // employee's name
                        strEmplNumber = brEmployee.ReadString();
                        strFName = brEmployee.ReadString();
                        strLName = brEmployee.ReadString();
                    }
                    finally
                    {
                        brEmployee.Close();
                        fsEmployee.Close();
                    }
    
                    Console.Write("Enter the start date of the " +
                                  "payroll you want to see (mm/dd/yyyy): ");
                    DateTime dteStartDate =
                        DateTime.Parse(Console.ReadLine());
    
                    // We want the month and day to include 0 if necessary
                    strMonth = dteStartDate.Month.ToString();
                    if (dteStartDate.Month < 10)
                        strMonth = "0" + dteStartDate.Month.ToString();
                    strDay = dteStartDate.Day.ToString();
                    if (dteStartDate.Day < 10)
                        strDay = "0" + dteStartDate.Day.ToString();
                    strYear = dteStartDate.Year.ToString();
    
                    strFilename = @"C:\Georgetown Cleaning " +
                                  @"Services\Payrolls\" +
                                  @"\" + strLName[0] +
                                  strFName[0] + strMonth +
                                  strDay + strYear + ".epr";
                    if (!File.Exists(strFilename))
                    {
                        Console.Write("{0}, {1} doesn't have a " +
                                      "payroll in that time frame",
                                      strLName, strFName);
                    }
                    else
                    {
                        // Open the payroll and display it
                        FileStream fsPayroll =
                            new FileStream(strFilename,
                                           FileMode.Open);
                        BinaryFormatter bfPayroll =
                            new BinaryFormatter();
                        PayrollInformation pay =
                            (PayrollInformation)bfPayroll.Deserialize(fsPayroll);
    
                        ShowPayroll(pay);
                    }
                }
            }
    
            public static void ShowPayroll(PayrollInformation payed)
            {
                . . . No Change
            }
        }
    }
  2. Execute the application and test it. Here is an example:
     
    ================================================
    Georgetown Cleaning Services
    ================================================
    
    What do you want to do?
    0. Quit
    1. Hire a new employee
    2. Process a payroll
    3. View an employee's payroll
    Your Choice: 3
    
    Enter Employee Number (00-000): 29-368
    Enter the start date of the payroll you want to see (mm/dd/yyyy): 11/12/2006
    
    ================================================
    =$= Payroll summary =$=
    ------------------------------------------------
    Employee #:    29-368
    Full Name:     Gertrude, Monay
    Hourly Salary: $10.85
    Start Period:  Sunday, November 12, 2006
    End Period:    Saturday, November 25, 2006
    ------------------------------------------------
            Monday Tuesday Wednesday Thursday Friday
    Week 1:  8.00   8.50     9.50      8.00    8.50
    Week 2:  6.50   7.00     8.00      6.00    7.00
    ------------------------------------------------
    Monetary Summary
                Hours   Amount
    Regular:    74.50   808.33
    Overtime:    2.50    40.69
    ------------------------------------------------
    Net Pay:   849.01
    ================================================
    
    
    What do you want to do?
    0. Quit
    1. Hire a new employee
    2. Process a payroll
    3. View an employee's payroll
    Your Choice: 3
    
    Enter Employee Number (00-000): 86-025
    Enter the start date of the payroll you want to see (mm/dd/yyyy): 11/26/06
    
    ================================================
    =$= Payroll summary =$=
    ------------------------------------------------
    Employee #:    86-025
    Full Name:     Anne, Harang
    Hourly Salary: $7.50
    Start Period:  Sunday, November 26, 2006
    End Period:    Saturday, December 09, 2006
    ------------------------------------------------
            Monday Tuesday Wednesday Thursday Friday
    Week 1:  8.00   6.50     8.50      8.00    8.00
    Week 2:  9.00   8.50     8.00      9.50    8.00
    ------------------------------------------------
    Monetary Summary
                Hours   Amount
    Regular:    79.00   592.50
    Overtime:    3.00    33.75
    ------------------------------------------------
    Net Pay:   626.25
    ================================================
    
    
    What do you want to do?
    0. Quit
    1. Hire a new employee
    2. Process a payroll
    3. View an employee's payroll
    Your Choice: 0
    
    Press any key to continue . . .
  3. Close the DOS window
 

Home Copyright © 2006 FunctionX, Inc.