473,657 Members | 2,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interface & Event

I have an interface ITest that includes an event TestStatusChang e.
There is also a class Test that implements ITest. In one of the
functions of Test I want to call the event (see code at the end) but
get the following error:

"The event 'eventTest.Test .TestStatusChan ged' can only appear on the
left hand side of += or -=.

All samples I saw seem to do the same I am doing. What am I missing?

Thanks

using System;
using System.Collecti ons.Generic;
using System.Text;

namespace eventTest
{
public delegate void TestStatus(Stri ng status);

interface ITest
{
event TestStatus TestStatusChang ed;
}

class Test : ITest
{
public event TestStatus TestStatusChang ed
{
add { TestStatusChang ed += value; }
remove { TestStatusChang ed -= value; }
}

public void Check()
{
TestStatusChang ed("ok"); //!!!!!!!!! COMPILE
ERROR !!!!!!!!!!!!!
}
}

class Program
{
static void Main(string[] args)
{
}
}
}

Apr 29 '07 #1
3 1685
First of all, it causes the stack overflow exception if you try to add
something to TestStatusChang ed. Because by the code below you simple
redefine the default "event" template behavior to add the value to
itself up to overflow exception.

public event TestStatus TestStatusChang ed
{
add { TestStatusChang ed += value; }
remove { TestStatusChang ed -= value; }
}

So, if you want your code to work then rewrite it just as:

public event TestStatus TestStatusChang ed;

or like:

class Test : ITest
{
private TestStatus _testStatus;

public event TestStatus TestStatusChang ed
{
add { _testStatus += value; }
remove { _testStatus -= value; }
}
}
But calling the _testStatus(..) , not the TestStatusChang ed(..) in this
case. Because it's what the "event" statement do behind
(http://www.codeproject.com/useritems/howeventswork.asp).

Regards, Alex Meleta
Blog:: http://devkids.blogspot.com
-----Original Message-----
From: hu*******@yahoo .com [mailto:hu****** *@yahoo.com]
Posted At: Montag, 30. April 2007 00:25
Posted To: microsoft.publi c.dotnet.langua ges.csharp
Conversation: Interface & Event
Subject: Interface & Event

I have an interface ITest that includes an event TestStatusChang e.
There is also a class Test that implements ITest. In one of the
functions of Test I want to call the event (see code at the end) but
get the following error:

"The event 'eventTest.Test .TestStatusChan ged' can only appear on the
left hand side of += or -=.

All samples I saw seem to do the same I am doing. What am I missing?

Thanks

using System;
using System.Collecti ons.Generic;
using System.Text;

namespace eventTest
{
public delegate void TestStatus(Stri ng status);

interface ITest
{
event TestStatus TestStatusChang ed;
}

class Test : ITest
{
public event TestStatus TestStatusChang ed
{
add { TestStatusChang ed += value; }
remove { TestStatusChang ed -= value; }
}

public void Check()
{
TestStatusChang ed("ok"); //!!!!!!!!! COMPILE
ERROR !!!!!!!!!!!!!
}
}

class Program
{
static void Main(string[] args)
{
}
}
}

Apr 29 '07 #2
public event TestStatus TestStatusChang ed
{
add { TestStatusChang ed += value; }
remove { TestStatusChang ed -= value; }
}
Unless you have a reason to use the more verbose syntax, change this
to just

public event TestStatus TestStatusChang ed;
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 29 '07 #3
hu*******@yahoo .com <hu*******@yaho o.comwrote:
I have an interface ITest that includes an event TestStatusChang e.
There is also a class Test that implements ITest. In one of the
functions of Test I want to call the event (see code at the end) but
get the following error:

"The event 'eventTest.Test .TestStatusChan ged' can only appear on the
left hand side of += or -=.

All samples I saw seem to do the same I am doing. What am I missing?
When you specify the event add/remove operations, you don't get the
autogenerated field.

See http://pobox.com/~skeet/csharp/events.html for more details.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 29 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3767
by: Ash | last post by:
Hello all, I am hoping this is the appropriate newsgroup for a C++ interface design question. I am trying to design an interface for a subscriber to register/deregister handlers for various events. The callbacks specified by the subscriber will be called when the events get trigerred in a different thread. Each event has different kinds of data associated with it. To achieve this I have the following: // The following describes the...
3
8213
by: Ohad Young | last post by:
Hi, I have an interface with an event. I'd like to explicitly implement the interface by a certain class. However, I received the following error: "An explicit interface implementation of an event must use property syntax" Here is a code sample: public interface IFoo
2
22302
by: Derrick | last post by:
How does one declare an event within an interface, so that every class which implements that interface must implement that event? I think I just need to specifiy the actual event as I would a function, but what about the delegate? Up to now, all my interfaces have only dealt with properties and functions. Here's basically what I want to do (for example): public interface IFoo
2
4984
by: Alex Sedow | last post by:
Why interface-event-declaration does not support multiple declarators like event-declaration? Grammar from C# spec: variable-declarators: variable-declarator variable-declarators "," variable-declarator variable-declarator:
2
2507
by: COLIN JACK | last post by:
Hi All, I've got a situation where I'm implementing an interface (BaseInterface in example below) and I want to use explicity interface implementation of an event so that I can add type safety. To see what I mean look at the example below where the class implementing the interface actually wants the event to be for a more specific delegate. Now this seems to work but the code, to me is unnecessarily ugly. This leaves me wondering if...
4
2891
by: Anthony Yott | last post by:
Anyone have an example of defining a custom event (i.e custom delegate) in an interface? -- Anthony Yott
8
2802
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);" TextBox2.Attributes.Add("onKeyPress", jscode) You will notice that jscode contains the JavaScript Logical And operator (&&). However, ASP.NET renders this as &amp;&amp; in the code that is
1
1101
by: Nathan Sokalski | last post by:
When implementing an interface in VB.NET using Visual Studio .NET 2005, it would be nice if I could have the method/function headers inserted automatically. Whenever we implement an event (such as Button.Click, TextBox.TextChanged, etc.), all we have to do is select the control and event from the dropdownlists at the top of the editor. Obviously the interfaces (IPostBackDataHandler, ICallbackEventHandler, etc.) are not listed there, but...
11
1286
by: Wayne Pedersen | last post by:
Need some help - and I may be doing this wrong, so please correct and suggest! I'm learning the MVP method, which I seem to have a good grasp of. Now I am trying something a bit more advanced. I'm declaring an event in a interface. I'm consuming the interface via another inherited Interface. I'd like to have my classes respond to this 'one' event... Based on my below example code, I'd like both Classes Test1 and Test2 to
0
8823
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
8503
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,...
0
7321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
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
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
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...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.