Tuesday, January 17, 2012

Delegates Real Example.....

static void Main()
{
var mycar = new Car(20, 100, "baachi");
mycar.RegisterWithCarEngine(OnCarEngineEvent);
//NOW SPEED UP THE CAR
for (int i = 0; i < 6; i++)
{
mycar.AccelerateSpeed(20);
Console.ReadLine();
}
}
public class Car
{
#region Properties
public string PetName { get; set; }
public int MaximumSpeed { get; set; }
public int CurrentSpeed { get; set; }
#endregion
public Car()
{
MaximumSpeed = 100;
}
public Car(int currentSpeed, int maximumSpeed, string petName)
{
CurrentSpeed = currentSpeed;
MaximumSpeed = maximumSpeed;
PetName = petName;
}
//Define delegate
public delegate void CarEngineHandler(string msgForCaller);
private CarEngineHandler _listOfHandlers;
private bool carIsDead;
//registration function for the caller
public void RegisterWithCarEngine(CarEngineHandler methodToCall)
{
_listOfHandlers = methodToCall;
}
public void AccelerateSpeed(int speed)
{
//If this car is dead Send dead message.......
if (carIsDead)
{
if (_listOfHandlers != null)
{
_listOfHandlers("Sorry BOSS : Car Is Dead: OOOOPS");
}
}
else
{
CurrentSpeed += speed;
// If this car is almost dead,
if (MaximumSpeed - CurrentSpeed == 10 && _listOfHandlers != null)
{
_listOfHandlers("Carefull Buddy: Gonna Blow:::");
}
}
if (CurrentSpeed > MaximumSpeed)
{
carIsDead = true;
}
else
{
Console.WriteLine("Current Speed is :{0}", CurrentSpeed);
}
}
}

Call back functions

A callback method is one which is passed as an argument from another method which is invoked due to some kind of event. The 'call back' nature of the argument is that it returns it's result to the method that provided it as an argument - that is to say that it 'calls back' with the return value of the callback method.//An innocuous looking method which will become known as a callback method
//because of the way in which we will invoke it.
int meaningOfLife(void) {
return 42;
}
//An innocuous looking method which just takes an int and prints it to screen
void Print_A_Number(int a_Number) {
System.out.print(a_Number);
}
//invoking a method which passes another method as an argument in reaction to an event (the 'another' method - meaningOfLife - is therefore called a callback method) and the event - main() - is that the program is starting
void main() {
Print_A_Number(meaningOfLife);
}
Callbacks are so-called due to their usage with pointer languages. If you don't use one of those, don't labour over the name 'callback'. Just understand that it is just a name to describe a method that's supplied as an argument to another method, such that when that method is called (whatever condition, such as a button click, a timer tick etc) the callback method is therefore invoked, which returns a result to the calling method, which in turn returns a result to the event.

Call back functions

A callback method is one which is passed as an argument from another method which is invoked due to some kind of event. The 'call back' nature of the argument is that it returns it's result to the method that provided it as an argument - that is to say that it 'calls back' with the return value of the callback method.//An innocuous looking method which will become known as a callback method
//because of the way in which we will invoke it.
int meaningOfLife(void) {
return 42;
}
//An innocuous looking method which just takes an int and prints it to screen
void Print_A_Number(int a_Number) {
System.out.print(a_Number);
}
//invoking a method which passes another method as an argument in reaction to an event (the 'another' method - meaningOfLife - is therefore called a callback method) and the event - main() - is that the program is starting
void main() {
Print_A_Number(meaningOfLife);
}
Callbacks are so-called due to their usage with pointer languages. If you don't use one of those, don't labour over the name 'callback'. Just understand that it is just a name to describe a method that's supplied as an argument to another method, such that when that method is called (whatever condition, such as a button click, a timer tick etc) the callback method is therefore invoked, which returns a result to the calling method, which in turn returns a result to the event.