Sign in to follow this  

[C#] Interfaces and events

This topic is 2845 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 an interface:
    public interface IQuadtree_Object
    {
        RigidBody2D body { get; }           // Body used in physics simulation (Quadtree will reallocate node this frame if the body is awake)
  
        void Draw();
        void Update(float dt);
    }

and a class that inherits from that interface that has an event parameter:
    public abstract class Entity2D : EventArgs, IQuadtree_Object    // Eventargs are required for events
    {
        protected Vector2 position;                                 // Central position of entity
        protected float rotation;                                   // Quadrangle/Sprite rotation
        protected Vector3 scale;                                    // Size/scale of entity

        public Effect Effect;                                       // Current graphical effect to draw sprite
        public Texture2D Texture;                                   // Current sprite texture

        protected RigidBody2D body;                                 // Rigid body associated with entity

        public event EventHandler<Entity2D> Click;                  // Event raised when the entity is selected


How can I invoke the event from the interface? Is it possible to have the event in the interface so that any classes that inherit from the interface, will also iherit the event? Thank you.

Share this post


Link to post
Share on other sites
Quote:
Original post by Spa8nky
Is it possible to have the event in the interface so that any classes that inherit from the interface, will also iherit the event?

Thank you.

Yes. :)

Their example:
public interface IShape
{
// Raise this event after drawing the shape.
event EventHandler OnDraw;
}


Share this post


Link to post
Share on other sites
Quote:

and a class that inherits from that interface that has an event parameter:

Strictly speaking, the proper term to use here is "implements" and not "inherits from."

Quote:

How can I invoke the event from the interface?

I'm not sure what you mean here; since interface's cannot have implementations themselves, it's not possible to invoke the event "from" any member of the interface itself. I assume you really mean for this to be the same question as the next one?

Quote:

Is it possible to have the event in the interface so that any classes that inherit from the interface, will also iherit the event?

If you define the event on the interface, classes implementing that interface will be required to implement the event; at which point you can implement that event on the class in question as normal, using the above-linked idiom.

Share this post


Link to post
Share on other sites

This topic is 2845 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