Closed Thread
Results 1 to 4 of 4

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

  1. #1
    mixpage65's Avatar
    mixpage65 is offline Learning Programmer
    Join Date
    Oct 2008
    Posts
    40
    Rep Power
    0

    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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Mar 2009
    Posts
    1,375
    Rep Power
    24

    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

  4. #3
    _autoboxing is offline Newbie
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

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

  5. #4
    Join Date
    Mar 2009
    Posts
    1,375
    Rep Power
    24

    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.

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. YASM Can anyone help me understand this asm code (It's short)
    By Renato_Motta in forum Assembly
    Replies: 9
    Last Post: 02-15-2011, 04:30 PM
  2. Formatting text (Short Code)
    By photoScan in forum C# Programming
    Replies: 0
    Last Post: 10-24-2010, 01:23 PM
  3. Is there a short code to run a problem like this ?
    By harddisk in forum C# Programming
    Replies: 6
    Last Post: 09-15-2010, 04:43 AM
  4. Intermediate A look at Delegates, Events & Event Driven Design in C#
    By PsychoCoder in forum CSharp Tutorials
    Replies: 0
    Last Post: 08-23-2010, 11:31 PM
  5. What does this short code do?
    By Java_Chick in forum Pascal and Delphi
    Replies: 18
    Last Post: 07-23-2008, 08:35 AM

Tags for this Thread

Bookmarks

Posting Permissions

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