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]); }
 }
}
}



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);
            }
        }
    }
}