By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,894 Members | 1,937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,894 IT Pros & Developers. It's quick & easy.

Delegate/Event HELL!

P: n/a
I need a little help please...

I'm simply trying to set up a very basic event for a class and then create
an event handler for that class in a Console application.

I think I'm very close, but I'm missing something. Here's what I've got...

//Abbreviated class code
using System;
namespace foo
{
public class fooFoo
{
public delegate void myEventEventHandler();
public event myEventEventHandler myEvent;

public void ChangeSpeed(short ByHowMuch)
{
if (test that determines if the event should be raised)
{
myEvent();
}
}
}
}
// *************************************************

//Console application that has reference to above class's assembly...
using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using foo;

namespace TestClass
{
internal sealed class Module1
{
// >--- An "Aside" question here ---<
//Why does the compiler force me to declare the following instance
as static?
//Is it because the class is "internal sealed"?
static fooFoo x = new fooFoo();

public static void Main()
{
//The following line is what won't compile...
//I get the following error message:
//Cannot implicitly convert type 'System.EventHandler' to
'foo.fooFoo.myEventEventHandler'
x.myEvent += new System.EventHandler(x_myEvent);
}

private static void x_myEvent(object sender, System.EventArgs e)
{
Console.WriteLine("");
}
}
}

Thanks for your help!

Sep 23 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Scott M." <No****@NoSpam.comwrote in message
news:u7**************@TK2MSFTNGP05.phx.gbl...
>I need a little help please...

I'm simply trying to set up a very basic event for a class and then create
an event handler for that class in a Console application.

I think I'm very close, but I'm missing something. Here's what I've
got...

I think "hell" is overstating things a bit. :)

Your main problem is simply that you are creating the wrong kind of event
handler. That's exactly what the error message is telling you.

Use "new fooFoo.myEventEventHandler(x_myEvent)" instead.

Of course, when you do you will find that you've declared the delegate
incorrectly, leaving out the parameters that you want passed. But that's a
different bug. :)

As far as why the compiler requires the x member to be declared static, I
believe it's because your class is implicitly static. That is, you didn't
declare the class as static, but it has no non-static members, and no public
constructor.

If you want the class to be instanced, I'd guess adding a public constructor
that does nothing would address that issue.

Pete
Sep 23 '06 #2

P: n/a
Thanks Peter! Yes, it has been hell, since I am coming from VB.NET where,
we don't have to deal with this at all to get an event up and running! It's
working now though.

Thanks.

"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:12*************@corp.supernews.com...
"Scott M." <No****@NoSpam.comwrote in message
news:u7**************@TK2MSFTNGP05.phx.gbl...
>>I need a little help please...

I'm simply trying to set up a very basic event for a class and then
create an event handler for that class in a Console application.

I think I'm very close, but I'm missing something. Here's what I've
got...


I think "hell" is overstating things a bit. :)

Your main problem is simply that you are creating the wrong kind of event
handler. That's exactly what the error message is telling you.

Use "new fooFoo.myEventEventHandler(x_myEvent)" instead.

Of course, when you do you will find that you've declared the delegate
incorrectly, leaving out the parameters that you want passed. But that's
a different bug. :)

As far as why the compiler requires the x member to be declared static, I
believe it's because your class is implicitly static. That is, you didn't
declare the class as static, but it has no non-static members, and no
public constructor.

If you want the class to be instanced, I'd guess adding a public
constructor that does nothing would address that issue.

Pete

Sep 23 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.