[C#] How do I send data from one class to another via delegates and events?

Started by
3 comments, last by Spa8nky 14 years, 1 month ago
If I have a class:

    class Foo
    {
        public int a = 1;
        public int b = 2;
        public float c = 10.1f;

        public Foo()
        {

        }
    }



and I want to send its parameters to another class that displays the parameters as a string. How can I do this using delegates and events? I've created a delegate outside of any class and under the common namespace:

    public delegate void OnClick(object sender);
I have created another class that will display the object name as a string:

    class InfoBar
    {
        public Color colour;
        public SpriteFont font;
        Rectangle quadrangle;
        public Texture2D texture;
        public bool visible;

        string objectSelectedName = "None";

        public InfoBar(Vector2 position, int width, int height, Texture2D texture, SpriteFont font, Color colour)
        {
            this.colour = colour;
            this.font = font;
            this.quadrangle = new Rectangle((int)position.X, (int)position.Y, width, height);
            this.texture = texture;
            visible = true;

            OnClick d1 = new OnClick(ClickHandler);
        }

        public void Draw(SpriteBatch sprite_Batch)
        {
            if (!visible)
            {
                return;
            }

            // Draw the info bar
            sprite_Batch.Draw(texture, quadrangle, colour);

            Vector2 position = new Vector2();
            position.X = quadrangle.Center.X;
            position.Y = quadrangle.Center.Y;
            sprite_Batch.DrawString(font, objectSelectedName, position, colour);
        }

        public void Update()
        {
            if (visible)
            {

            }
        }

        /// <summary>
        /// Event handler for when an object is selected
        /// </summary>
        void ClickHandler(object sender)
        {
            objectSelectedName = sender.ToString();
        }
    }



How do I get Foo to send itself and therefore its available parameters to InfoBar when invoking InfoBar's OnClick? I've studied the MSDN tutorial here but I can't figure out what is going on. Can anyone please help?
Advertisement
class FooEventArgs : EventArgs{    public int A { get; private set; }    public int B { get; private set; }    public float C { get; private set; }    public FooEventArgs(int a, int b, float c)    {        A = a;        B = b;        C = c;    }}class Foo{    int a = 1;    int b = 2;    float c = 10.1f;    public event EventHandler<FooEventArgs> Click;    public Foo()    {    }    protected virtual void OnClick(FooEventArgs e)    {        if (Click != null)            Click(this, e);    }}Foo f = new Foo();f.Click += infoBar.ClickHandler;
Mike Popoloski | Journal | SlimDX
Thanks very much Mike, this is beginning to make sense now.

The line:

Click += InfoBar.ClickHandler;

returns an error for me as I have done something incorrect:

No overload for 'ClickHandler' matches delegate 'System.EventHandler<Cthonian.FooEventArgs>'

I have made the ClickHandler in InfoBar public:

        /// <summary>        /// Event handler for when an object is selected        /// </summary>        public void ClickHandler(EventArgs e)        {        }


Where have I gone wrong?
Quote:Original post by Spa8nky
No overload for 'ClickHandler' matches delegate 'System.EventHandler<Cthonian.FooEventArgs>'


If we look at the documentation...

Quote:
[SerializableAttribute]public delegate void EventHandler<TEventArgs>(    Object sender,    TEventArgs e)where TEventArgs : EventArgs


Translation: EventHandler<T>s take two parameters: One of type 'Object' and one of type 'T'.

Quote:public void ClickHandler(EventArgs e)

This has 1 parameter.
Thank you very much both of you for helping me understand delegates and events in this situation.

I've changed the Click event handler, for when the object is selected, by adding the missing parameter and making it static:

        /// <summary>        /// Event handler for when an object is selected        /// </summary>        public static void ClickHandler(Object sender, EventArgs e)        {            FooEventArgs f = (FooEventArgs)e;            objectSelectedName = f.A.ToString();        }


I've also cast EventArgs to FooEventArgs in order to retrieve its parameters. This has taught me that all classes used with events should inherit from EventArgs.

I hope that I have done this correctly, time to practice this subject with more classes!

This topic is closed to new replies.

Advertisement