Wednesday, March 19, 2014

Serialization And De-Serialization in C Sharp Using Arrays




Create a serializable class with two arrays. One for rate of the advertisement and other for running time of the advertisement. Serialize the object of this class to a stream using Soap formatter. Create a desktop application that asks the user for the start time and the number of minutes for which the advertisement has to be run. De-serialize the object from the stream and retrieve the values of the rates for specific time. Calculate the amount of money the user has to pay for running a particular advertisement.


using System;
usingSystem.Collections.Generic;
using System.IO;
usingSystem.Runtime.Serialization.Formatters.Soap;
usingSystem.Windows.Forms;
usingSystem.Xml.Serialization;

namespace WindowsFormsApplication2
{
    [Serializable]
publicpartialclassTaxRate : Form
    {
int[] array1 = newint[2];
int[] array2 = newint[2];
publicTaxRate()
        {
InitializeComponent();}
int a = 0;
privatevoid button3_Click(object sender, EventArgs e)
        {for (inti = 0; i< array1.Length; i++)
            {
if (array1[i] == Convert.ToInt32(UserSalarytextBox.Text))
                {a = array1[i] / 100 * array2[i];
                }
            }label4.Text = Convert.ToString(a);
        }
inti = 0;        
privatevoid button1_Click(object sender, EventArgs e)
        {
array1[i] = Convert.ToInt32(SalarytextBox.Text);
array2[i] = Convert.ToInt32(TaxtextBox.Text);
i++;
        }
privatevoid button2_Click(object sender, EventArgs e)
        {for (inti = 0; i< array1.Length; i++)
            {
Salary.Items.Add(array1[i]);
            }
for (inti = 0; i< array2.Length; i++){
Tax.Items.Add(array2[i]); } }
privatevoid button4_Click(object sender, EventArgs e)
        {
Stream stream1 = File.OpenWrite("D:\\binaryserialization1.txt");
Stream stream2 = File.OpenWrite("D:\\binaryserialization2.txt");
SoapFormattersf = newSoapFormatter();
sf.Serialize(stream1, array1);
sf.Serialize(stream2, array2);
stream1.Close();
stream2.Close();
        }privatevoid button5_Click(object sender, EventArgs e)
        {
FileStream stream1 = newFileStream("D:\\binaryserialization1.txt", FileMode.Open);
FileStream stream2 = newFileStream("D:\\binaryserialization2.txt", FileMode.Open);
SoapFormatterformate = newSoapFormatter();
            array1 = formate.Deserialize(stream1) asint[];
            array2 = formate.Deserialize(stream2) asint[];
for (inti = 0; i< array1.Length; i++)
            {
Salary.Items.Add(array1[i]);
            }
for (inti = 0; i< array2.Length; i++)
            {
Tax.Items.Add(array2[i]); }
 }
}
}



No comments:

Post a Comment