+ Reply to Thread
Results 1 to 4 of 4

Thread: Is this the right use of Delegates of Events ? (short code)

  1. #1
    Learning Programmer mixpage65 is an unknown quantity at this point mixpage65's Avatar
    Join Date
    Oct 2008
    Posts
    40

    Is this the right use of Delegates of Events ? (short code)

    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 senderCreateCarEventArgs e);

        
    // create car event publisher
        
    public class CreateCarPublisher
        
    {
            public 
    BadEngineConfig onBadEngineConfig;
            
            public 
    void CreateCar(String engineConfigString Id)
            {
                if (
    engineConfig != "V8")
                {
                    
    CreateCarEventArgs e = new CreateCarEventArgs();
                    
    e.Id Id;
                    if (
    onBadEngineConfig != null)
                        
    onBadEngineConfig(thise);
                }
            }
        }

        
    //client class that creates a car
        
    public class CreateCarClient
        
    {
            
    CreateCarPublisher publisher = new CreateCarPublisher();

            public 
    void BadEngine(Object senderCreateCarEventArgs e)
            {
                
    System.Console.WriteLine("Bad engine of car with ID: " e.Id);

            }

            public 
    CreateCarClient(String engineConfigString Id)
            {            
                
    publisher.onBadEngineConfig += new BadEngineConfig(BadEngine);

                
    publisher.CreateCar(engineConfigId);
            }
        }
        
        
    //tester
        
    public class TestDelegates
        
    {
            public static 
    void Main(string[] args)
            {
                
    CreateCarClient aCar = new CreateCarClient("V6""1111");
                
    Console.ReadKey();
            }
        }



  2. #2
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,374

    Re: Is this the right use of Delegates of Events ? (short code)

    Well, after brief looking at this sub-link I can tell it's correct.
    Delegates and Events in C# / .NET

  3. #3
    Newbie _autoboxing is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    3

    Re: Is this the right use of Delegates of Events ? (short code)

    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 09:06 AM. Reason: Please use code tags when you are posting your codes !

  4. #4
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,374

    Re: Is this the right use of Delegates of Events ? (short code)

    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.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Replies: 0
    Last Post: 09-02-2009, 07:40 PM
  2. Replies: 0
    Last Post: 08-18-2009, 12:50 AM
  3. Replies: 1
    Last Post: 08-03-2009, 01:19 PM
  4. Replies: 0
    Last Post: 11-27-2008, 01:30 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts