Need help with Generic delegatges, event handlers and methods.

M

moondaddy

I'm trying to learn how to use generic delegates, event handlers and
methods. To do so I built a small sample Console app to see how this can
work. I got it working using the EventHandler<T> from the system name space
but with out using a generic method to pass into my delegate.

By the way. EventHandler<T> doesn't seem so generic as it uses 'object
sender' as the first parameter.

Next I converted the method to a generic method and created my own generic
event handler 'MyEventHandler<T>' that is generic. Problem is that I can't
get this to compile. Rather than trying to explain where and how its not
compiling, I'll just zip and attach the project. Can someone please see why
its not compiling and make recommendations on the most appropriate way to
create a generic delegate and method?

Thanks.
 
L

Lloyd Dupont

what about a simple sample of code highlighting your problem?

in any case here is a sample of working pseudo code:

public delegate void MyEventHandler<SENDER, EVENTARG>(SENDER sender,
EVENTARG event) where EVENTARG : EventArg;

public class FooEventArg: EventArg
{
}
public class Foo
{
public event MyEventHandle<Foo, FooEventArg> AFoolishEvent;
protectd void OnFoolishEvent(FooEventArg e)
{
if(AFoolishEvent != null)
AFoolishEvent(this, e);
}
}

--
Regards,
Lloyd Dupont

NovaMind development team
NovaMind Software
Mind Mapping Software
<www.nova-mind.com>
 
C

Carl Daniel [VC++ MVP]

moondaddy said:
I'm trying to learn how to use generic delegates, event handlers and
methods. To do so I built a small sample Console app to see how this
can work. I got it working using the EventHandler<T> from the system
name space but with out using a generic method to pass into my
delegate.
By the way. EventHandler<T> doesn't seem so generic as it uses 'object
sender' as the first parameter.

Next I converted the method to a generic method and created my own
generic event handler 'MyEventHandler<T>' that is generic. Problem
is that I can't get this to compile. Rather than trying to explain
where and how its not compiling, I'll just zip and attach the
project. Can someone please see why its not compiling and make
recommendations on the most appropriate way to create a generic
delegate and method?

Looks like you forgot to attach it - or whatever you used to post stripped
the attachment.

-cd
 
L

Lloyd Dupont

I think you need to read the error messages.
They are quite explicit;

for example the first one:
'Error 1 The type 'ConsoleApplication1.myEvents.MyEventArgs' must be
convertible to 'ConsoleApplication1.IBusinessBase' in order to use it as
parameter 'T' in the generic type or method
'ConsoleApplication1.MyEventHandler<T>'
C:\Lloyd-Dev\C#\PracticeWritingEvents\PracticeWritingEvents\ParentClass.cs
12 40 PracticeWritingEvents
'

Indeed:
1. ConsoleApplication1.myEvents.MyEventArgs do not inherit from
IBusinessBase'

2. T should inherit from IBusinessBase as per the definition of
OnMRaiseMyEvent

I think the code is unnecessary complex and convoluted for its simple
purpose, hence you get your self lost in it......

--
Regards,
Lloyd Dupont

NovaMind development team
NovaMind Software
Mind Mapping Software
<www.nova-mind.com>
 
L

Lloyd Dupont

Generally speaking good coding avoid unnecessary complexity whenever
possible.
But you might be just trying to learn here, so I could understand ;-)

--
Regards,
Lloyd Dupont

NovaMind development team
NovaMind Software
Mind Mapping Software
<www.nova-mind.com>
 

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

Top