Sign in to follow this  

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

This topic is 2852 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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;

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

This topic is 2852 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this