Microsoft KB Archive/821175

= You may receive a CS0117 compiler error when you explicitly implement an interface event =

Article ID: 821175

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# 2005 Express Edition

-





SYMPTOMS
You can implement interface events in a class explicitly. However, when you implement interface events explicitly, and you instantiate the class instance, you may receive the following compiler error message:

error CS0117: ' ' does not contain a definition for '



CAUSE
When a class explicitly implements events of an interface, explicitly implemented events are not accessible from class instances.



WORKAROUND
To work around this problem, create an instance of the interface, and then access the events.

Use the following code to create an instance of the interface to access events: IMyInterface eventSample = new ExplicitEventSample; eventSample.MyEvent += new MyDelegate(FindLength);



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
  Paste the following code in Notepad: using System;

namespace EventTest {   public delegate int MyDelegate(string s);

public interface IMyInterface {       event MyDelegate MyEvent; }

public class ExplicitEventSample: IMyInterface {       event MyDelegate IMyInterface.MyEvent //Explicit implementation of IMyInterface.MyEvent {           add {               MyEvent2Storage += value; }           remove {               MyEvent2Storage -= value; }       }

public MyDelegate MyEvent2Storage; // underlying storage for IMyInterface.MyEvent.

}

class Class1 {       [STAThread] static int FindLength(string strArg) {           return strArg.Length; }       static void Main(string[] args) {          ExplicitEventSample eventSample = new ExplicitEventSample;

eventSample.MyEvent += new MyDelegate(FindLength); }   } }  Save the file as c:\Sample.cs .  At the Visual Studio .NET command prompt, run the following command to compile the code: csc    c:\Sample.cs 

You may receive the compilation error that is mentioned in the &quot;Symptoms&quot; section of this article.

