Monday, March 17, 2014

Serialization and De-Serialization in C Sharp




Create a Serializable class that stores tax rate and tax limit. Serialize the object of this class to a stream using Soap formatter. Create a desktop application that asks the monthly salary from the user and calculates the amount of tax he/she has to pay. De-serialize the object from the stream and get the value of tax limit that was stored. If the annual salary of the user exceeds the limit then calculate the amount of tax otherwise the user does not have to pay any tax.

using System;
usingSystem.Collections.Generic;
using System.IO;
usingSystem.Runtime.Serialization.Formatters.Binary;
usingSystem.Windows.Forms;
namespace WindowsFormsApplication2
{
    [Serializable]
publicpartialclassForm1 : Form
    {
public Form1()
        {
InitializeComponent();
        }
Dictionary<int, int>dic = newDictionary<int, int>();
privatevoid button1_Click(object sender, EventArgs e)
        {
dic.Add(Convert.ToInt32(TimetextBox.Text),Convert.ToInt32(RatetextBox.Text));
        }
privatevoid button2_Click(object sender, EventArgs e)
        {
foreach (var item indic)
            {
Times.Items.Add(item.Key);
Rates.Items.Add(item.Value);
            }
        }
int a = 0;
privatevoid button3_Click(object sender, EventArgs e)
        {
foreach (var item indic)
            {
if (Convert.ToInt32(textBox1.Text) == item.Key)
                {
                    a = item.Key * item.Value * 6;
                }
            }
            label4.Text = Convert.ToString(a);
        }
privatevoid button4_Click(object sender, EventArgs e)
        {
Streamstream = File.OpenWrite("D:\\binaryserialization1.txt");
BinaryFormatterfm = newBinaryFormatter();
fm.Serialize(stream,dic);
stream.Close();  }
privatevoid button5_Click(object sender, EventArgs e)
        {
FileStream stream = newFileStream("D:\\binaryserialization1.txt",FileMode.Open);
BinaryFormatterformate = newBinaryFormatter();

dic = formate.Deserialize(stream) asDictionary<int, int>;
foreach (var item indic)
            {
Times.Items.Add(item.Key);
Rates.Items.Add(item.Value);
            }
        }
    }
}



No comments:

Post a Comment