Jump to content
  • Advertisement
Sign in to follow this  
Spa8nky

[C#] Interfaces and events

This topic is 3065 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!