CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2015
    Posts
    59

    Question events are not heritable, are they?

    There is literature saying events are not heritable.
    I made an code example, seems to me events a r e heritabel.

    Maybe its something not really clear within oop world?
    The example has 3 classes: cycel, bicycle, motorcycle and InvalidWhightEventArgs.

    Cycel is the base, and there is declared the event InvalidWightEventHandler, occures if whight gets an invalid value.

    The property whight is overwritten in the other classes, with intention. Cause the literature says futhermore, events rise only in those classes where they are declared.
    So, the event in this example is only declared in cycle, but raises in all the others as well.

    The code, Class Cycle:
    Code:
    public delegate void InvalidWightEventHandler(Cycle sender);
    
        public class Cycle
        {
            protected double mWight = 0;
          
            public event InvalidWightEventHandler InvalidWight;
    
            //Constructor
            public Cycle() { }
    
            //OnXx Methods
            protected virtual void OnInvalidWight(InvalidWhightEventArgs e)
            {
                if (InvalidWight != null)
                    InvalidWight(this);
            }
            //Propertys
            public virtual double Wight
            {
                get
                {return mWight;}
                set
                {
                    if (value >= 0)
                       {mWight = value;}
                    else
                    {
                        OnInvalidWight(new InvalidWhightEventArgs(value));
                    }
                }
            }
    
            public override string ToString()
            {
                return "Cycle=" + "Cycle\n" + ", Wight=" + Wight;
            }
        }
    Class BiCycle
    Code:
     public class BiCycle : Cycle
        {
            //Constructor
            public BiCycle() { }
    
            //Propertys
            public override double Wight
            {
                get
                {
                    return mWight;
                }
                set
                {
                    //other conditions
                    if (value >= 10)
                    {
                        mWight = value;
                    }
                    else
                    {
                        OnInvalidWight(new InvalidWhightEventArgs(value));
                    }
                }
            }
            public override string ToString()
            {
                return "Cycle=" + "BiCyle\n" + ", Wight=" + Wight;
            }
        }
    Class MotorCycle
    Code:
    public class MotorCycle: BiCycle
        {
            //Constructor
            public MotorCycle() { }      
    
            //Propertys
            public override  double Wight
            {
                get
                {return mWight;}
                set
                {
                    //MotorCycle other conditions
                    if (value >= 100)
                       {mWight = value; }
                    else
                    { 
                        OnInvalidWight(new InvalidWhightEventArgs(value));
                    }
                }
            }
            public override string ToString()
            {
                return "Cycle=" + "MotorCyle\n" + ", Wight=" + Wight;
            }
        }
    Class InvalidWhightEventArgs
    Code:
     public class InvalidWhightEventArgs : EventArgs
        {
            // Fields
            private double mInvalidWhight;
    
            //Constructor
            public InvalidWhightEventArgs(double invalidWhight)
            {
                mInvalidWhight = invalidWhight;
            }
    
            //Propertys
            public double InvalidWhight
            {
                get { return mInvalidWhight; }
            }
        }
    Class Programm
    Code:
     class Program
        {
            static void Main(string[] args)
            {
                Cycle myC = new Cycle();
                BiCycle myB = new BiCycle();
                MotorCycle myM = new MotorCycle();
    
                myC.InvalidWight += myB_InvalidWight;
                myB.InvalidWight += myB_InvalidWight;
                myM.InvalidWight += myB_InvalidWight;
    
                myC.Wight = -1;
                myB.Wight = 1;
                myM.Wight = 10;
    
                Console.ReadLine();  
            }
    
            private static void myB_InvalidWight(object sender)
            {           
                Console.WriteLine("invalid wight: " + ((Object)sender).ToString());                     
            }
        }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: events are not heritable, are they?

    While the InvalidWhightEventArgs class is being used in classes that are inherited, the InvalidWhightEventArgs class itself is not inherited.

  3. #3
    Join Date
    Aug 2015
    Posts
    59

    Re: events are not heritable, are they?

    Understand, but this is to me the event "public event InvalidWightEventHandler InvalidWight".
    And I can call it here, as I call methods or protertys from a baseclass.

    myB.InvalidWight...

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: events are not heritable, are they?

    Give us a link to where you read that events are not inheritable (or quote the exact text of what you read). Also what .net version was the text referring to and what .net version are you using?
    Last edited by Arjay; October 27th, 2016 at 01:30 AM.

  5. #5
    Join Date
    Aug 2015
    Posts
    59

    Re: events are not heritable, are they?

    Its from a german autor. I understand might be better to read it from the base, but, place his name here... I dont know, nowadays its like you put him on public turture. And its finally not available in english, never saw it.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: events are not heritable, are they?

    Quote Originally Posted by pschulz View Post
    Its from a german autor. I understand might be better to read it from the base, but, place his name here... I dont know, nowadays its like you put him on public turture. And its finally not available in english, never saw it.
    Ok. How about answering >> Also what .net version was the text referring to and what .net version are you using?

  7. #7
    Join Date
    Aug 2015
    Posts
    59

    Re: events are not heritable, are they?

    well, have fogotten.
    Used this example with VS2010 or VS2012, the text refers to VS2012

  8. #8
    Join Date
    Aug 2015
    Posts
    59

    Re: events are not heritable, are they?

    I redused the code, so its without "InvalidWhightEventArgs "
    which means, there is no way to use the event within BiCycle.
    But the event its still usabel for Bicycle, same way as before.
    myB.InvalidWight += myB_InvalidWight;
    So long I dont need any diffrent in property wight, its fine.

    From this perspective...yeah, might be possible to say, its not heritable
    Code:
     public delegate void InvalidWightEventHandler();
    
        public class Cycle
        {
            protected double mWight = 0;
    
            public event InvalidWightEventHandler InvalidWight;
    
            //Constructor
            public Cycle() { }
            
            //Propertys
            public virtual double Wight
            {
                get
                { return mWight; }
                set
                {
                    if (value >= 0)
                    { mWight = value; }
                    else
                    {   // Ereignis auslösen
                        InvalidWight();
                    }
                }
            }
    
            public override string ToString()
            {
                return "Cycle=" + "Cycle\n" + ", Wight=" + Wight;
            }
        }
    Code:
    static void Main(string[] args)
            {
                Cycle myC = new Cycle();
                BiCycle myB = new BiCycle();
    
                myC.InvalidWight += myB_InvalidWight;
                myB.InvalidWight += myB_InvalidWight;
    
                myB.Wight =- 1;
                myC.Wight = -1;          
    
                Console.ReadLine();
            }
            private static void myB_InvalidWight()
            {
                Console.WriteLine("invalid wight: ");
            }

  9. #9
    Join Date
    Aug 2015
    Posts
    59

    Question Re: events are not heritable, are they?

    Quote Originally Posted by Arjay View Post
    what .net version was the text referring to and what .net version are you using?
    Hey Arjay, are you really really really sure it makes a different, carring this subject and using Vs2010 or 2012 or 2015 ??

    If it sounds like I try to insult you, please let me know, i change it, I promise

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: events are not heritable, are they?

    Folks probably aren't going to get much out of a thread where folks take 6 or 7 weeks to answer what they've already answered. Closing the thread.
    Last edited by Arjay; December 11th, 2016 at 09:32 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured