Home All Groups Group Topic Archive Search About
Author
29 Apr 2007 8:25 PM
hufaunder@yahoo.com
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)
        {
        }
    }
}

Author
29 Apr 2007 9:37 PM
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

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
29 Apr 2007 11:00 PM
Jon Skeet [C# MVP]
hufaun***@yahoo.com <hufaun***@yahoo.com> wrote:
> 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

AddThis Social Bookmark Button