Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / April 2007

Tip: Looking for answers? Try searching our database.

Interface & Event

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
hufaunder@yahoo.com - 29 Apr 2007 21:25 GMT
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)
       {
       }
   }
}
Mattias Sjögren - 29 Apr 2007 22:37 GMT
>        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

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Jon Skeet [C# MVP] - 30 Apr 2007 00:00 GMT
> I have an interface ITest that includes an event TestStatusChange.
> There is also a class Test that implements ITest. In one of the
[quoted text clipped - 5 lines]
>
> 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.

Signature

Jon Skeet - <skeet@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


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.