This code runs but i want to know if this is the right use of delegates of events.
When a car with an engine other than V8 is created, the event "onBadEngineConfig" is executed. Theoretically this is right but i want to know if this is the way to do it.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test_project
{
// Create cars with v8 engines only
public class CreateCarEventArgs : System.EventArgs
{
public String Engine;
public String Id;
public CreateCarEventArgs()
{
Engine = "V8";
}
}
// delegate declaration
public delegate void BadEngineConfig(Object sender, CreateCarEventArgs e);
// create car event publisher
public class CreateCarPublisher
{
public BadEngineConfig onBadEngineConfig;
public void CreateCar(String engineConfig, String Id)
{
if (engineConfig != "V8")
{
CreateCarEventArgs e = new CreateCarEventArgs();
e.Id = Id;
if (onBadEngineConfig != null)
onBadEngineConfig(this, e);
}
}
}
//client class that creates a car
public class CreateCarClient
{
CreateCarPublisher publisher = new CreateCarPublisher();
public void BadEngine(Object sender, CreateCarEventArgs e)
{
System.Console.WriteLine("Bad engine of car with ID: " + e.Id);
}
public CreateCarClient(String engineConfig, String Id)
{
publisher.onBadEngineConfig += new BadEngineConfig(BadEngine);
publisher.CreateCar(engineConfig, Id);
}
}
//tester
public class TestDelegates
{
public static void Main(string[] args)
{
CreateCarClient aCar = new CreateCarClient("V6", "1111");
Console.ReadKey();
}
}
}
Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum