Go Back   CodeCall Programming Forum > Software Development > C# Programming
Register Blogs Search Today's Posts Mark Forums Read

C# Programming C# (pronounced C-sharp) is a new object oriented language from Microsoft and is derived from C and C++. It also borrows a lot of concepts from Java too including garbage collection.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-23-2009, 05:37 AM
mixpage65's Avatar
Learning Programmer
 
Join Date: Oct 2008
Posts: 40
mixpage65 is an unknown quantity at this point
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.

PHP 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();
        }
    }


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 09-25-2009, 09:33 AM
ArekBulski's Avatar
Guru
 
Join Date: Mar 2009
Posts: 1,373
ArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really nice
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-30-2009, 07:18 PM
Newbie
 
Join Date: Sep 2009
Posts: 3
_autoboxing is an unknown quantity at this point
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 10:06 AM.. Reason: Please use code tags when you are posting your codes !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-01-2009, 07:27 AM
ArekBulski's Avatar
Guru
 
Join Date: Mar 2009
Posts: 1,373
ArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really nice
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
F# First Class Events – Async Workflows + Events Part II Kernel News 0 09-02-2009 08:40 PM
F# First Class Events – Creating Events Kernel News 0 08-18-2009 01:50 AM
The spirit of MVVM (ViewModel), it’s not a code counting exercise. Kernel News 1 08-03-2009 02:19 PM
Back to Basics: Delegates, Anonymous Methods and Lambda Expressions Kernel News 0 11-27-2008 02:30 PM


All times are GMT -5. The time now is 09:40 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0