473,387 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Declare an Event within an Interface

How does one declare an event within an interface, so that every class which
implements that interface must implement that event? I think I just need to
specifiy the actual event as I would a function, but what about the
delegate?

Up to now, all my interfaces have only dealt with properties and functions.

Here's basically what I want to do (for example):

public interface IFoo
{
//Properties:
bool HasItems{get;}
Item[] GetItems{get;}

//Functions:
void AddItem(Item i);

//Events: --How do I do this? Where do I define the delegate?
void OnItemAdded(Item i);

}

Thanks,

Derrick
Nov 16 '05 #1
2 22286
Try this.
namespace MyNamespace
{
public delegate void MyDelegate();

public interface IMyInterface
{
event MyDelegate MyEvent;
}

public class MyClass : IMyInterface
{
public event EII.MyDelegate MyEvent;
}
}

HTH.

Artur
Derrick wrote:
How does one declare an event within an interface, so that every class which
implements that interface must implement that event? I think I just need to
specifiy the actual event as I would a function, but what about the
delegate?

Up to now, all my interfaces have only dealt with properties and functions.

Here's basically what I want to do (for example):

public interface IFoo
{
//Properties:
bool HasItems{get;}
Item[] GetItems{get;}

//Functions:
void AddItem(Item i);

//Events: --How do I do this? Where do I define the delegate?
void OnItemAdded(Item i);

}

Thanks,

Derrick

Nov 16 '05 #2
Hi Derrick,

You declare the events in the same way as you normaly do for classes. Don't
forget that events are pare (or three) methods - add, remove and invoke (the
latter is not supported by C#) that's why interfaces can have events

so you declare your interface as follows

class ClickedEventArgs
{

}

delegate void ClickedEventHandler(object sender, ClickedEventArgs e);

interface IFoo
{
event ClickedEventHandler Clicked;
}

In the interface declaration you don't declare any delegate data members
(because you cannot declare data members in interface), you just say that
classes implementing that interface have to declate this event.

Then you can implement the interface and the event as one normally do
class Foo:IFoo
{
public event ClickedEventHandler Clicked;

protected virtual void OnClicked(ClickedEventArgs e)
{
if(Clicked != null)
Clicked(this, e);
}
}

Or you gen go more advanced and provide some logic in add and remove
accessors

class Bar:IFoo
{

public event ClickedEventHandler Clicked
{
add
{
//Add a handler
}
remove
{
//Remove the handler
}
}
protected virtual void OnClicked(ClickedEventArgs e)
{
//Raise the event
}
}

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Derrick" <de***********@hotmail.com> wrote in message
news:vO*********************@news20.bellglobal.com ...
How does one declare an event within an interface, so that every class which implements that interface must implement that event? I think I just need to specifiy the actual event as I would a function, but what about the
delegate?

Up to now, all my interfaces have only dealt with properties and functions.
Here's basically what I want to do (for example):

public interface IFoo
{
//Properties:
bool HasItems{get;}
Item[] GetItems{get;}

//Functions:
void AddItem(Item i);

//Events: --How do I do this? Where do I define the delegate?
void OnItemAdded(Item i);

}

Thanks,

Derrick

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Divick | last post by:
Hi all, does any one know what is the right way to forward declare classes within namespaces. Though I have been using the syntax as follows but it doesn't sound good to me. namespace...
0
by: sqlboy2000 | last post by:
All, I have a simple datagrid with one column being a template column that contains a dropdown control. I'd like to be able capture when the index of the dropdown changes and access the value that...
4
by: Anthony Yott | last post by:
Anyone have an example of defining a custom event (i.e custom delegate) in an interface? -- Anthony Yott
2
by: BenLeino | last post by:
Hi there, I have a custom interface with event declarations in it. One of my classes implements this interace and also its events. When i make a new instance from outside the class (with...
1
by: Anthony Yott | last post by:
Anyone have an example of defining a custom event (i.e custom delegate) in an interface? -- Anthony Yott
1
by: Tim.Forbess | last post by:
Using asp.net 2.0 I have a web project with one page which contains 2 user controls. One user control is a message bar with a label control and the other user control that contains a treeview...
1
by: webgour | last post by:
Hello, I would like to create an onload event within my object. The following works fine : function TEST() { this.image= new Image(); } TEST.prototype.Initialize = function()
3
by: Wildster | last post by:
Hi, I want to put an event within an event but it seems as though this can't be done and I need to figure a work around. Basically I am trying to say that if the user clicks on the...
2
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, i have a working example of a simple event delegation: http://pastebin.com/m104a6c1f can someone please show me how to define my custom event in an interface? thanks, rodchar
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.