Longhorn People & Groups

As told by Oliver Fisher, Lead Software Design Engineer

CS0071

We ran into a problem yesterday that took a while to figure out.  It was one of those programming issues where you know there are about 3 lines of code you need to write, but figuring out which 3 lines takes a while.

We had two interfaces with events with the same name and the same object needed to implement both.  We knew this wasn't a problem for methods so we suspected that it wasn't a problem for events either.  We just had to figure out the C# syntax.

Our first attempt went something like this:

class MyClass : NS1.IFoo, NS2.IBar
{
    event NS1.MyEvent NS1.IFoo.EventName;
    event NS2.MyEvent NS2.IBar.EventName;
} 

That produced compiler error CS0071: An explicit interface implementation of an event must use property syntax.  A quick search on MSDN got us some sample code. Too bad the sample code was incorrect.  I've filed a bug with MSDN.

Based on the incorrect sample code, attempt number two was roughly:

class MyClass : NS1.IFoo, NS2.IBar
{
    event NS1.MyEvent NS1.IFoo.EventName
    {
        get { return null; }
        set { }
    }
} 

That produced compiler error CS1055: An add or remove accessor expected.  This time MSDN was more helpful and gave us the correct syntax, although the code still didn't actually work.

class MyClass : NS1.IFoo, NS2.IBar
{
    event NS1.MyEvent NS1.IFoo.EventName
    {
        add { EventName += value; }
        remove { EventName -= value; }
    }
    void FireEventName()
    {
        if (EventName != null) { EventName(); }
    }
} 

As we typed the code, it seemed like it wasn't going to work.  The add accessor calls the add accessor which calls the add accessor, etc.  Interestingly, the compiler didn't castigate us for that but it did complain about our attempt to fire the event with CS0079: The event can only appear on the left hand side of += or -=.

This time MSDN's sample code gave use the full picture and we used a couple of private events.  The final code went something like this.

class MyClass : NS1.IFoo, NS2.IBar
{
    private event NS1.MyEvent InternalEvent1;
    private event NS2.MyEvent InternalEvent2;
    public event NS1.MyEvent NS1.IFoo.EventName
    {
        add { InternalEvent1 += value; }
        remove { InternalEvent1 -= value; }
    }
    public event NS2.MyEvent NS2.IBar.EventName
    {
        add { InternalEvent2 += value; }
        remove { InternalEvent2 -= value; }
    }
    void FireEvents()
    {
        if (InternalEvent1 != null) { InternalEvent1(); }
        if (InternalEvent2 != null) { InternalEvent2(); }
    }
} 

You probably already knew all of this.  But my inept Google search didn't really find much (but I can never find anything) and Inside C# doesn't talk about this.  Maybe this will be helpful, although I still have my doubts that anyone will ever read this blog...

The process did confirm two beliefs.  First, MSDN contains all the information that you need if you can just find it.  Second, the makers of C# are way smart and have thought of everything. Well, probably not everything or they wouldn't be working on Whidbey.

Published Friday, November 07, 2003 7:10 AM by darkcanuck

Comments

 

Andrew Boardman said:

I'm reading it, man. I'm reading it.
November 13, 2003 6:16 PM
 

Oliver Fisher said:

MSDN still hasn't updated their incorrect sample code. I'll poke them again...
February 20, 2004 1:15 PM
 

Alex said:

me to :)
March 30, 2004 4:52 AM
 

Drazen said:

I read it. I will use the idea.
May 14, 2004 12:56 PM
 

Keith said:

Dude, it's guys like you that save fellas like me from having to figure this out on my own, all over again. Thanks for this info...
July 20, 2004 9:25 AM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker