473,606 Members | 2,381 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 myEventEventHan dler();
public event myEventEventHan dler myEvent;

public void ChangeSpeed(sho rt 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.Collecti ons;
using System.Data;
using System.Diagnost ics;
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.EventHa ndler' to
'foo.fooFoo.myE ventEventHandle r'
x.myEvent += new System.EventHan dler(x_myEvent) ;
}

private static void x_myEvent(objec t sender, System.EventArg s e)
{
Console.WriteLi ne("");
}
}
}

Thanks for your help!

Sep 23 '06 #1
2 2032
"Scott M." <No****@NoSpam. comwrote in message
news:u7******** ******@TK2MSFTN GP05.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.myEventE ventHandler(x_m yEvent)" 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*********@Nn OwSlPiAnMk.comw rote in message
news:12******** *****@corp.supe rnews.com...
"Scott M." <No****@NoSpam. comwrote in message
news:u7******** ******@TK2MSFTN GP05.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.myEventE ventHandler(x_m yEvent)" 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
4814
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 call some delegate, in which thread this delegate gets called - callers = synchronous, or some other thread = asynchronous? And in what thread is this done with "event"? Ondra.
3
2605
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
1
6415
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 As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event. I don't want to raise the event, but rather pass the delegate associated with an event to another method.
4
2007
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 of the functions it refers to. So I've been playing around and came up with he following code. I know it doesn't do much, but I just wanted to fit together delegates and parameter passing: //////////////// Code start
5
2099
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 created. I have a delegate / event that passes via EventArgs the appropriate info from the grid to the UserControl2. This works fine, except for now I'm a little lost on the final step....I need that once the UserControl2 is "filled out" that it is...
3
2316
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: p1.FirstNameChanged -= new Person.NameChanged(p1_FirstNameChanged); The above line that removes the delegate confuses me. It works - buy why must we use the 'new' keyword? The delegate already exists and is "registered" with the event. I would think...
7
1770
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 the piece of code which doesn't appear here, but abstracts the btnAccept_Click method?
6
10404
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 type, I have created a generic event args class that looks like this: public class ItemChangingEventArgs<T> { ... }
3
1396
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 implements member callbacks: typedef bool (__cdecl IEventHandler::*lbEvHandler)(IUnknown* uk);
0
8016
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7955
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8440
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8096
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5966
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3937
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2448
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1557
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1300
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.