Subhash Sharma

Subhash Sharma
Subhash Sharma

This is Subhash Sharma(Software Engineer) Blog

Welcome to this blog and find every solution.............

Search This Blog

Software Engineer(Subhash Sharma)

Software Engineer(Subhash Sharma)
Software Engineer

Friday, March 12, 2010

Events and Delegates

//Delegate is handler for the event. Delegate work without event but Event can't work without Delegate because Delegate work as a handler for Event.

Car on = new Car();
private void car_Change()
{
MessageBox.Show("Tankempty");
}
private void sum(int a, int b)
{
MessageBox.Show("Tankempty");
}
private void car_Change1()
{
MessageBox.Show("TankFull");
}

private void button1_Click(object sender, EventArgs e)
{
on.Change += new Car.ChangingHandler(car_Change);
on.Change1 += new Car.ChangingHandler(car_Change1);
on.SetTank(int.Parse(textBox1.Text));



}

//Class

public class Car
{
private int tank;

// delegate declaration
public delegate void ChangingHandler();

// event declaration
public event ChangingHandler Change;
public event ChangingHandler Change1;

public Car()
{

}

public void SetTank(int p)
{
this.tank = p;
// call the event
if (p < 51)
{
Change();
}
else
{
Change1();
}
}

public string GetTank()
{
return this.tank.ToString();
}
}

No comments:

Post a Comment