[Mono-dev] inherited event

Vladimir Giszpenc vgiszpenc at dsci.com
Wed Feb 25 13:54:51 EST 2009


Hi,

 

I was wondering why events have to be in local scope to be called.  If
the event is in a base class, shouldn't I be able to invoke it?

This is not a bug in Mono as the behavior is the same on MS.Net, but I
was hoping for some insight.

 

Thanks,

 

Vlad

 

namespace TestEvents

{

    public abstract class Foo

    {

        protected event EventHandler OnBar;

 

        public event EventHandler Bar

        {

            add

            {

                lock (OnBar)

                {

                    this.OnBar += value;

                }

            }

            remove

            {

                lock (OnBar)

                {

                    this.OnBar -= value;

                }

            }

        }

    }

    public class ConcreteFoo : Foo

    {

        public void DoStuff()

        {

            if (null!=this.OnBar) //Why can't I do this?

            {

                this.OnBar(this, new EventArgs());

            }

        }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            Foo foo = new ConcreteFoo();

            foo.Bar += new EventHandler(foo_Bar);

            //...

            ((ConcreteFoo)foo).DoStuff();

        }

 

        static void foo_Bar(object sender, EventArgs e)

        {

            Console.WriteLine("FOOBAR happened");

        }

    }

}

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20090225/a91dd5fc/attachment.html 


More information about the Mono-devel-list mailing list