Interface & Event

H

hufaunder

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)
{
}
}
}
 
A

Alex Meleta

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: (e-mail address removed) [mailto:[email protected]]
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)
{
}
}
}
 
M

Mattias Sjögren

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
 
J

Jon Skeet [C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top