The event 'X' can only appear on the left hand side of += or -= (except when used from within the type 'X')#

Assume you have a base class and another class derived from it. You have events defined in base class so you wish to use them in the derived class.

    class a
    {
        public delegate void Amanindelegate();
        public event Amanindelegate Amanin;
    }

    class b:a
    {
        public void hey0()
        {
            Amanin();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            b b1=new b();
            b1.Amanin+=new a.Amanindelegate(b1_Amanin);
            b1.hey0();
        }
        static void b1_Amanin()
        {
            Console.WriteLine("aboovv");
        }
    }

Here you have the mentioned error in hey0 method line Amanin();
The best practice is as follows.

    class a
    {
        public delegate void Amanindelegate();
        public virtual event Amanindelegate Amanin;
    }

    class b:a
    {
        public override event a.Amanindelegate Amanin;
        public void hey0()
        {
            Amanin();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            b b1=new b();
            b1.Amanin+=new a.Amanindelegate(b1_Amanin);
            b1.hey0();
        }
        static void b1_Amanin()
        {
            Console.WriteLine("aboovv");
        }
    }

 

Thursday, March 12, 2009 6:46:52 PM (GTB Standard Time, UTC+02:00) #    Comments [0]  | 

 

All content © 2012, Levent YILDIZ
On this page
This site
Calendar
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
Archives
Sitemap
Blogroll OPML
Disclaimer

Powered by: newtelligence dasBlog 2.3.9074.18820

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts


Pick a theme: