Monday, July 26, 2010

C# Tip - 7/25/2010

I am falling in love with events.

Events are implemented via delegates and are used for objects that communicate with multiple clients when a particular action occurs.

Scenario:  there's a soccer game between England and Mexico.  Both countries need to be informed when a goal was scored. 

Step 1: create a class for goal event data.

public class GoalEventArgs : EventArgs

{
  public readonly string Country;
  public readonly string Player;
  public readonly int minute;

  public GoalEventArgs(string country, string player, int minute)
  {
    this.Country = country;
    this.Player = player;
    this.minute = minute;
  }
}

Step 2: create a delegate for the event handler.
 
public delegate void AddGoalEventHandler(object sender, GoalEventArgs e);


Step 3: create the game class that will handle the event (make it static since only one game can be played at a time).

class Game
{
  public event AddGoalEventHandler GoalEventHandler;
  private static Game _instance = null;
  static Game()
  {
    _instance = new Game();
  }

  private Game() { }

  public static Game Singleton
  {
    get
    {
      return _instance;
    }
  }

  public void GoalScored(string country, string player, int minute)
  {
    AddGoalEventHandler g = GoalEventHandler;
    if (g != null)
      g(null, new GoalEventArgs(country, player, minute));
  }
}

Step 4: create a class for the Mexicans.
 
class Mexico
{
  private static Game game;

  public Mexico(Game g)
  {
    game = g;
    game.GoalEventHandler += new AddGoalEventHandler(game_GoalEventHandler);
  }
 
  void game_GoalEventHandler(object sender, GoalEventArgs e)
  {
    if (e.Country.ToUpper().Equals("ENGLAND"))
      Console.WriteLine("Ohh NO!!! ENGLAND JUST SCORED!!! That bastardo " + e.Player + " scored on minute " + e.minute.ToString());
    else
      Console.WriteLine("GOOOOAAAAALLLL!! VIVA MEXICO!!! " + e.Player + " has done it once again on minute " + e.minute.ToString());
  }
}

Step 5: create a class for the English.

class England
{
  private static Game game;

  public England(Game g)
  {
    game = g;
    game.GoalEventHandler += new AddGoalEventHandler(game_GoalEventHandler);
  }

  void game_GoalEventHandler(object sender, GoalEventArgs e)
  {
    if (e.Country.ToUpper().Equals("ENGLAND"))
      Console.WriteLine("ENGLAND JUST SCORED!!! That hero" + e.Player + " scored on minute " + e.minute.ToString());
    else
      Console.WriteLine("NOOOOO!!! " + e.Player + " has done the unthinkable and scored on minute " + e.minute.ToString());
  }
}

To test, add the following to your main method:

Game g = Game.Singleton;

Mexico mexico = new Mexico(g);
England england = new England(g);
g.GoalScored("england", "owen", 15);
g.GoalScored("mexico", "hernandez", 25);

Your console output will be the following after line 4 gets executed:
Your console output will be the following after line 5 gets executed:

As you can see, the possibilities with events are limitless!

No comments:

Labels