Creating my events.

D

Daniel Groh

How and when should I create my own event ?

When i do this:
public delegate void myEvent(object sender, System.EventArgs e);

What am I doing ?

I tryed to find some sample on net but nothing was enough to understand when
using! =/

Can someone help me ?

--

Atenciosamente,

Daniel Groh
CTF Technologies do Brasil Ltda.
Analista Programador
Fone: 11 3837-4203
E-mail: (e-mail address removed)
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You are declaring the "template" or the signature of the method that will
handle an event of that kind.

The delegate is the first part you need, the other is the event declaration
itself and then you need a method which must have the same parameters that
the delegated you declared before.

There is a very good explanation of events in the MSDN , just looks for
events in the MSDN index



cheers,
 
J

Jianwei Sun

Daniel said:
How and when should I create my own event ?

When i do this:
public delegate void myEvent(object sender, System.EventArgs e);

What am I doing ?

[You are creating a new delegate type, so later on, you can do that in
your class. I have to say it's bad naming.

U better delcare
public delegate void MyEventType(object sender, System.EventArg e);

so you can delcare an event using the type you just created.


class MyEventSource
{
.....
public event MyEventType myEvnet;
....

// you can raise the event in any place
void Function RaiseMyEvent()
{
myEvnet( sender, e );
}
}


And then you can register the event in the intersed source in way looks
like:

class MyEventSink
{
MyEventSource myEventSource;

this.myEventSource.myEvent+=new MyEventType(EventHandlerFunction);


public void EventHandlerFunction(object sender, System.EventArg e)
{
// do what ever you like
}
}

I tryed to find some sample on net but nothing was enough to understand when
using! =/

[There are a lot of samples on the Internet, and you need practice the
samples yourself to be able to understand it].
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

SqlParameter ??? 5
inherited classes 4
Unable to find script library 1
Context.Handler ? 1
AutoEventWireup 2
RegExp for Data 1
EventLog does not write 2
Delegates and Events 8

Top