473,503 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interface & Event

I have an interface ITest that includes an event TestStatusChange.
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.TestStatusChanged' 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.Collections.Generic;
using System.Text;

namespace eventTest
{
public delegate void TestStatus(String status);

interface ITest
{
event TestStatus TestStatusChanged;
}

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

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

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

Apr 29 '07 #1
3 1676
First of all, it causes the stack overflow exception if you try to add
something to TestStatusChanged. 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 TestStatusChanged
{
add { TestStatusChanged += value; }
remove { TestStatusChanged -= value; }
}

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

public event TestStatus TestStatusChanged;

or like:

class Test : ITest
{
private TestStatus _testStatus;

public event TestStatus TestStatusChanged
{
add { _testStatus += value; }
remove { _testStatus -= value; }
}
}
But calling the _testStatus(..), not the TestStatusChanged(..) 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.public.dotnet.languages.csharp
Conversation: Interface & Event
Subject: Interface & Event

I have an interface ITest that includes an event TestStatusChange.
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.TestStatusChanged' 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.Collections.Generic;
using System.Text;

namespace eventTest
{
public delegate void TestStatus(String status);

interface ITest
{
event TestStatus TestStatusChanged;
}

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

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

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

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

public event TestStatus TestStatusChanged;
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*******@yahoo.comwrote:
I have an interface ITest that includes an event TestStatusChange.
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.TestStatusChanged' 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.com>
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
3757
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...
3
8205
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...
2
22291
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...
2
4977
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 ","...
2
2502
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. ...
4
2881
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
2781
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);"...
1
1096
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...
11
1272
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. ...
0
7192
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,...
0
7064
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...
0
7261
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,...
0
7315
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6974
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...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
1492
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 ...
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
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...

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.