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();
}
}
}
Well, after brief looking at this sub-link I can tell it's correct.
Delegates and Events in C# / .NET
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
This is an incorrect way of using delegates and events.
your delegate is public! your CreateCarClient could easily invoke the delegate directly
for example:
publisher.onBadEngineConfig(this, new CreateCarEventArgs());
this is just wrong, you should know better.
correct way: define an event:
public event EventHandler<CreateCarEventArgs> OnCreateCarEvent;
notify registered users
Code:private void NotifyCarEventCreated(OnCreateCarEvent e) { if(null != OnCreateCarEvent) { OnCreateCarEvent(this, e); } } if you have an instance o34 and you wish to register: o34.OnCreateCarEvent += new EventHandler<CreateCarEventArgs>(SomeMethod); private void SomeMethod(object o, CreateCarEventArgs e) { }
Last edited by Jaan; 10-01-2009 at 07:06 AM. Reason: Please use code tags when you are posting your codes !
Mixpage- I had a long break from .NET now so I am not sure of what I want to say, so let me throw the cards and let you figure it out, okay?
I think your code is correct, that is you are using delegate variable. I think you can assign only 1 method to it, no more no less but one.
Autoboxing- Events and delegates can be different things. Event variable can hold from none to many delegates assigned. If you invoke it, all of them will be executed. But you can still use delegates as single procedure holders.
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks