Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Delegates and inheritance

Delegates and inheritance

Scheduled Pinned Locked Moved C#
oophelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BadKarma
    wrote on last edited by
    #1

    Hi, I have the following strange behaviour I have an abstract base class in which a delegate is availlable. I the derived class i need to call the connected function. But that results in the following compliation error: The event 'winFromsTest.MyBase.Callback' can only appear on the left hand side of += or -= (except when used from within the type 'winFromsTest.MyBase') Does anybody knows a reasson why i can't call the delegate from the derived class('s) codito ergo sum

    R 1 Reply Last reply
    0
    • B BadKarma

      Hi, I have the following strange behaviour I have an abstract base class in which a delegate is availlable. I the derived class i need to call the connected function. But that results in the following compliation error: The event 'winFromsTest.MyBase.Callback' can only appear on the left hand side of += or -= (except when used from within the type 'winFromsTest.MyBase') Does anybody knows a reasson why i can't call the delegate from the derived class('s) codito ergo sum

      R Offline
      R Offline
      Robert Rohde
      wrote on last edited by
      #2

      Because you have declared it as an event which is protected from invalid access. If you want to raise the event from an inheriting class then add a protected method to your base class as a helper function:

      //base class
      protected virtual void OnCallback(EventArgs e){
      if (Callback != null)
      Callback(this, e);
      }

      //somewhere in inheriting class
      base.OnCallback(EventArgs.Empty);

      B 1 Reply Last reply
      0
      • R Robert Rohde

        Because you have declared it as an event which is protected from invalid access. If you want to raise the event from an inheriting class then add a protected method to your base class as a helper function:

        //base class
        protected virtual void OnCallback(EventArgs e){
        if (Callback != null)
        Callback(this, e);
        }

        //somewhere in inheriting class
        base.OnCallback(EventArgs.Empty);

        B Offline
        B Offline
        BadKarma
        wrote on last edited by
        #3

        Thanks, So it just like i just found out, to prevent you to shoot yourself in the foot :-D codito ergo sum

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups