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



Thursday, March 13, 2014

Resourcees Writer and Management Class in C Sharp




Make a Windows form application in which manage and write resources by using Resources.

ResourceWriter Code:

usingSystem.Resources;
namespacewriting_resources
{
classProgram
    {
staticvoid Main(string[] args)
        {
try
            {
ResourceWriterrw = newResourceWriter("References.resources");
Imageimg;
img = Image.FromFile("Butterfly.jpg");
rw.AddResource("FirstImage", img);
rw.AddResource("FirstName", "First");
rw.AddResource("FirstPhone", "00112233");
img = Image.FromFile("Child.jpg");
rw.AddResource("SecondImage", img);
rw.AddResource("SecondName", "Second");
rw.AddResource("SecondPhone", "23423");
img = Image.FromFile("Bike.jpg");
rw.AddResource("ThirdImage", img);
rw.AddResource("ThirdName", "Third");
rw.AddResource("ThirdPhone", "2134");
img = Image.FromFile("Smile.jpg");
rw.AddResource("FourthImage", img);
rw.AddResource("FourthName", "Fourth");
rw.AddResource("FourthPhone", "4134");
Console.WriteLine("Resources Successfully writed");
rw.Close();
            }
catch (Exception ex)
            {
Console.WriteLine(ex.Message);
            }
        }
    }
}




Resource Manager Code:

usingSystem.Resources;
usingSystem.Reflection;
usingSystem.Collections;

namespaceResourcesManager
{
publicpartialclassForm1 : Form
    {
public Form1()
        {
InitializeComponent();
        }
ResourceManagerrm;
privateAssembly ass;
privateArrayList phones = newArrayList();
privatevoid button1_Click(object sender, EventArgs e)
        {
ass = Assembly.GetExecutingAssembly();
rm = newResourceManager("ResourcesManager.References", ass);
for (int count = 1; count<=4; count++)
            {
this.list_name.Items.Add(rm.GetString(count+"Name"));
this.img_List.Images.Add((Image)rm.GetObject(count+"Image"));

this.phones.Add(rm.GetString(count+"Phone"));
            }
pic_image.Image=this.img_List.Images[0];
this.list_name.SelectedIndex=0;
this.phone_list.Text=(string)this.phones[0];
this.btnnext.Enabled=true;
        }
privatevoidlist_name_SelectedIndexChanged(object sender, EventArgs e)
        {
this.phone_list.Text = (string)this.phones[this.list_name.SelectedIndex];
this.pic_image.Image=this.img_List.Images[this.list_name.SelectedIndex];
        }
privatevoidbtnnext_Click(object sender, EventArgs e)
        {
int index = this.list_name.SelectedIndex;
if (index < 4)
            {
index += 1;
this.list_name.SelectedIndex = index;
            }
else
            {
index+=0;
this.list_name.SelectedIndex=index;
            }}
privatevoid Form1_Load(object sender, EventArgs e){
        }
    }