473,385 Members | 1,707 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,385 software developers and data experts.

Delegate/Event HELL!

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
2 2017
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ondrej Sevecek | last post by:
Hello, what is the difference between "event" and the only use of delegate? Why one should note events with "event" keyword when the functionality seems the same as with pure delegates? When I...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
1
by: Kerry Jenkins | last post by:
I am having problems passing an Event Delegate as an argument to a method that accepts a delegate argument. I get the following error message: 'Public Event ProgressChanged(sender As Object, e...
4
by: ^MisterJingo^ | last post by:
Hi all, I've been trying to get my head around delegates. The book i'm using had a single example, not much explaination, and didn't show how to set up a delegate and pass variables in and out...
5
by: Doug Handler | last post by:
Hi, I have a form (Form1) that contains a tab control which one tab has a customer user control (UserControl1). When the user double-clicks on the grid hosted there a new user control is...
3
by: Jeff S | last post by:
Please consider this sample code: It registers a delegate with an event. p1.FirstNameChanged += new Person.NameChanged(p1_FirstNameChanged); Now the following code removes the delegate:...
7
by: Ant | last post by:
Hello, Very simple question but one I need clarified. Which part of the statement below is considered the 'delegate'? Is it the 'new System.EventHandler' or the btnAccept_Click? or is it...
6
by: David Veeneman | last post by:
I have several events that pass a value in their event args. One event passes an int, another a string, another a DateTime, and so on. Rather than creating a separate set of event args for each...
3
by: lothar.behrens | last post by:
Hi, I am thinking about the delegate mechanism and try to understand it. I am coming from C++ and know about callbacks or member callbacks. In C++ I have this typedef for every class that...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.