Click here to Skip to main content
15,887,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a uncompleted code for calling event accessor, it is almost finished except to call it. Hopefully it can be filled.
C#
class Program
    {
        static void Main(string[] args)
        {
            Pub p = new Pub();
            p.Raise();
        }
    }
    public class Pub
    {
        public event EventHandler<MyArgs> onChange = delegate { };
        public event EventHandler<MyArgs> OnChange
        {
            add
            {
                lock(onChange)
                {
                    onChange += value;
                }
            }
            remove
            {
                lock (onChange)
                {
                    onChange -= value;
                }
            }
        }
        public void Raise()
        {
            onChange(this, new MyArgs(42));
        }
    }
    public class MyArgs : EventArgs
    {
        public MyArgs(int value)
        {
            Value = value;
        }
        public int Value { get; set; }
    }
Posted
Comments
CHill60 7-Jan-15 11:44am    
Let us know if you have any problems when you've filled in the code
[no name] 7-Jan-15 13:07pm    
I want to verify it on the screen. Say something like Console.WriteLine("Event raised: {0}",42);. I don't know where is the place to put it. By the way, I used the lock is for thread safe. The code of class Pub is from a book, the rest of it is mine.

Here's an alternate version that I use in teaching:
C#
using System;

namespace Code_Samples
{
    public class EventDemo
    {
        // custom EventArgs
        public class EventDemoArgs : EventArgs
        {
            public int someInt { private set; get; }
            public string someString { private set; get; }

            public EventDemoArgs(int anint, string astring)
            {
                someInt = anint;
                someString = astring;
            }
        }

        // private Properties
        private int SomeInt {  set; get; }

        private string SomeString { set; get; }

        // ctor
        public EventDemo(int anint, string astring)
        {
            SomeInt = anint;
            SomeString = astring;
        }

        // shortcut for Delegate syntax in the case of an Event with custom data
        
        /* MSDN: The EventHandler<TEventArgs> delegate is a predefined delegate that
        represents an event handler method for an event that generates data.
        The advantage of usingEventHandler<TEventArgs> is that you do not need
        to code your own custom delegate if your event generates event data.
        You simply provide the type of the event data object as the generic parameter.*/
        
        public event EventHandler<EventDemoArgs> DemoEvent;

        // void method used to test firing Event
        public void TestEvent()
        {
           OnDemoEvent(new EventDemoArgs(SomeInt, SomeString));
        }

        // virtual method to dispatch the Event to all subscribed handlers
        protected virtual void OnDemoEvent(EventDemoArgs e)
        {
            EventHandler<EventDemoArgs> handler = DemoEvent;
            
            if (handler != null) handler(this, e);
        }
    }
}
A sample test use:
C#
public void Test()
{
    Code_Samples.EventDemo eDemo = new Code_Samples.EventDemo(999, "test event demo");
    
    // test with multiple handlers
    eDemo.DemoEvent += eDemo_DemoEvent1;   
    eDemo.DemoEvent += eDemo_DemoEvent2;
    
    // raise Test Event on class instance
    eDemo.TestEvent();
}

private void eDemo_DemoEvent1(object sender, Code_Samples.EventDemo.EventDemoArgs e)
{
    MessageBox.Show(string.Format("handler 1: {0} {1}", e.someString, e.someInt));
}

private void eDemo_DemoEvent2(object sender, Code_Samples.EventDemo.EventDemoArgs e)
{
    MessageBox.Show("handler 2 ...");
}
 
Share this answer
 
v3
Comments
[no name] 7-Jan-15 16:07pm    
Cool. +5.
Don't mark onChange as event. If you mark it as event, you cannot call it as you tried. Also, it's not necessary to use lock while adding an event handler. And if OnChange is public, it's not necessary to make onChange also public, unless you need to use it at another location (but I don't think you do, because you already have OnChange). So, replace it with this:
C#
private EventHandler<MyArgs> onChange;
public event EventHandler<MyArgs> OnChange
{
    add
    {
        onChange += value;
    }
    remove
    {
        onChange -= value;
    }
}

And to call it:
C#
public void Raise()
{
    if (onChange != null)
    {
        onChange(this, new MyArgs(42));
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900