How to call Events from Interfaces C#?

Eibach

Limp Gawd
Joined
Feb 4, 2007
Messages
230
So i have a design problem. I have a mouse class that has delegets and events. ie MouseButtonPressed, MouseMoved. and such that are getting called by a state engine.

What i want to have happen is to create an interface like IClickable or IDraggable or somthing and have events inside those interfaces that get called when the mouse event gets called.

Code:
public interface IClickable

    public event MouseDevice.ButtonClickedHandler<MouseButtons, MouseState> Clicked;

then in the MouseDevice class it has

Code:
public delegate void ButtonClickedHandler<O, S>(object sender, InputDeviceEventArgs<O, S> e);

and

Code:
public event ButtonClickedHandler<MouseButtons, MouseState> ButtonClicked;

So basically I want to have Clicked be called when buttonClicked gets called.

Is there a way to do this?
 
Hey.. I think I can help you...

First off, there's no way to execute the delegate associated with an event in an interface. To do so, you have to write some logic, which an interface does not implement any logic. You could implement some base logic in an abstract class, because abstract classes allow you to write methods and provide some base functionality and also define abstract methods which must be overriden similar to interfaces. However, you most likely do not want to implement these as abstract classes, because I believe your class to implement more than one of these interfaces at a time, correct? Abstract classes are classes, and C# only supports single inheritance, similar to java.

Anyhow, so what do i suggest? Well it is somewhat difficult to see exactly what you are trying to achieve, you could define interface methods like IClickable has OnClick...etc.

Maybe something like

if(variableInQuestion is IClickable)
(variableInQuestion as IClickable).OnClick(/* event args go here */ );

Also your question reminds me of the IDropTarget interface which exists in .NET for items such as the ToolStripItem class.. you may want to check out the MSDN on it
 
Hey.. I think I can help you...

First off, there's no way to execute the delegate associated with an event in an interface. To do so, you have to write some logic, which an interface does not implement any logic. You could implement some base logic in an abstract class, because abstract classes allow you to write methods and provide some base functionality and also define abstract methods which must be overriden similar to interfaces. However, you most likely do not want to implement these as abstract classes, because I believe your class to implement more than one of these interfaces at a time, correct? Abstract classes are classes, and C# only supports single inheritance, similar to java.

Anyhow, so what do i suggest? Well it is somewhat difficult to see exactly what you are trying to achieve, you could define interface methods like IClickable has OnClick...etc.

Maybe something like

if(variableInQuestion is IClickable)
(variableInQuestion as IClickable).OnClick(/* event args go here */ );

Also your question reminds me of the IDropTarget interface which exists in .NET for items such as the ToolStripItem class.. you may want to check out the MSDN on it

Hey thanks for the reply. I have tried to implement something slimier to what you have but it does not let me specify (variableInQuestion as IClickable).OnClick( /* Event args/* ).
 
Also your question reminds me of the IDropTarget interface which exists in .NET for items such as the ToolStripItem class.. you may want to check out the MSDN on it

Hey so i was looking at the documentation and I cant seem to figure out how they do it exactly though all they say is.

"The OnDragDrop method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class."

this is kinda what i want to do only i want to have an event in my interface.

Because here is what im going to be doing.

Code:
public class Button : IClickable
{
//Comes with Iclickable
public event MouseDevice.ButtonClickedHandler<MouseButtons, MouseState> Clicked;

}

public class TitleScreen : GameScreen
{
Button btnExit;

InitializeScreen()
{
btnExit = new Button();

btnExit.Clicked += new MouseDevice.ButtonClickedHandler<MouseButtons, MouseState>(exit_Clicked);
}

void exit_Clicked(object sender, InputEventArgs<MouseButtons, MouseState>(object o, InputEventArgs<MouseButtons, MouseState> e)
{
//Close this screen.
}
}

thanks for the help
 
Back
Top