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. Hide an event in an inherited class

Hide an event in an inherited class

Scheduled Pinned Locked Moved C#
helptutorialquestion
10 Posts 5 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
    bobo jede
    wrote on last edited by
    #1

    Hi, I have an interesting problem (interesting for me :)). I need to hide an event in an inherited class. Example: Base class: ComboBox Inherited class: MyCombo And I want the event SelectedIndexChanged to be "disabled" out of MyCombo class. So that it shloud be impossible to write: MyCombo mc = new MyCombo(); mc.SelectedIndexChanged += new EventHandler(mc_SelectedIndexChanged); I tried this: **private new** event EventHandler SelectedIndexChanged; but it doesn't work - it is still possible to use this event. Is there any way to do this? And is it correct to do it in OOP? Thanx, Bobo

    E J L 3 Replies Last reply
    0
    • B bobo jede

      Hi, I have an interesting problem (interesting for me :)). I need to hide an event in an inherited class. Example: Base class: ComboBox Inherited class: MyCombo And I want the event SelectedIndexChanged to be "disabled" out of MyCombo class. So that it shloud be impossible to write: MyCombo mc = new MyCombo(); mc.SelectedIndexChanged += new EventHandler(mc_SelectedIndexChanged); I tried this: **private new** event EventHandler SelectedIndexChanged; but it doesn't work - it is still possible to use this event. Is there any way to do this? And is it correct to do it in OOP? Thanx, Bobo

      E Offline
      E Offline
      Ed Poore
      wrote on last edited by
      #2

      I havn't heard of a way to do this, maybe you could mark the event as Obsolete?


      Formula 1 - Short for "F1 Racing" - named after the standard "help" key in Windows, it's a sport where participants desperately search through software help files trying to find actual documentation. It's tedious and somewhat cruel, most matches ending in a draw as no participant is able to find anything helpful. - Shog9 Ed

      B 1 Reply Last reply
      0
      • B bobo jede

        Hi, I have an interesting problem (interesting for me :)). I need to hide an event in an inherited class. Example: Base class: ComboBox Inherited class: MyCombo And I want the event SelectedIndexChanged to be "disabled" out of MyCombo class. So that it shloud be impossible to write: MyCombo mc = new MyCombo(); mc.SelectedIndexChanged += new EventHandler(mc_SelectedIndexChanged); I tried this: **private new** event EventHandler SelectedIndexChanged; but it doesn't work - it is still possible to use this event. Is there any way to do this? And is it correct to do it in OOP? Thanx, Bobo

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #3

        bobo.jede wrote:

        Is there any way to do this?

        Yes, write your own control, rather than inheriting ComboBox.

        bobo.jede wrote:

        And is it correct to do it in OOP?

        Quite simply, no it is not. Inheritance implies that the derived object inherits every public property/method/event from its parent.

        --- How to get answers to your questions[^]

        B 1 Reply Last reply
        0
        • E Ed Poore

          I havn't heard of a way to do this, maybe you could mark the event as Obsolete?


          Formula 1 - Short for "F1 Racing" - named after the standard "help" key in Windows, it's a sport where participants desperately search through software help files trying to find actual documentation. It's tedious and somewhat cruel, most matches ending in a draw as no participant is able to find anything helpful. - Shog9 Ed

          B Offline
          B Offline
          bobo jede
          wrote on last edited by
          #4

          You mean Obsolete attribute? I've tried it, but it didn't help :( Thanx for your answer Bobo

          E A 2 Replies Last reply
          0
          • J J4amieC

            bobo.jede wrote:

            Is there any way to do this?

            Yes, write your own control, rather than inheriting ComboBox.

            bobo.jede wrote:

            And is it correct to do it in OOP?

            Quite simply, no it is not. Inheritance implies that the derived object inherits every public property/method/event from its parent.

            --- How to get answers to your questions[^]

            B Offline
            B Offline
            bobo jede
            wrote on last edited by
            #5

            J4amieC wrote:

            Yes, write your own control, rather than inheriting ComboBox.

            Yes, that can be a way :) but I want to use this control and it's rich functionality.

            J4amieC wrote:

            Quite simply, no it is not. Inheritance implies that the derived object inherits every public property/method/event from its parent.

            But I can do the same with methods, don't I ?! Bobo

            J 1 Reply Last reply
            0
            • B bobo jede

              J4amieC wrote:

              Yes, write your own control, rather than inheriting ComboBox.

              Yes, that can be a way :) but I want to use this control and it's rich functionality.

              J4amieC wrote:

              Quite simply, no it is not. Inheritance implies that the derived object inherits every public property/method/event from its parent.

              But I can do the same with methods, don't I ?! Bobo

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              bobo.jede wrote:

              But I can do the same with methods, don't I

              No, you can never completly hide a method in a derived class either. I suppose you could 'new' it and just do nothing within the method, but its still there.

              --- How to get answers to your questions[^]

              1 Reply Last reply
              0
              • B bobo jede

                Hi, I have an interesting problem (interesting for me :)). I need to hide an event in an inherited class. Example: Base class: ComboBox Inherited class: MyCombo And I want the event SelectedIndexChanged to be "disabled" out of MyCombo class. So that it shloud be impossible to write: MyCombo mc = new MyCombo(); mc.SelectedIndexChanged += new EventHandler(mc_SelectedIndexChanged); I tried this: **private new** event EventHandler SelectedIndexChanged; but it doesn't work - it is still possible to use this event. Is there any way to do this? And is it correct to do it in OOP? Thanx, Bobo

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #7

                Try overriding OnSelectedIndexChanged and DONT call the base class.

                **

                xacc.ide-0.2.0 preview - Now in 100% C# goodness

                **

                B 1 Reply Last reply
                0
                • L leppie

                  Try overriding OnSelectedIndexChanged and DONT call the base class.

                  **

                  xacc.ide-0.2.0 preview - Now in 100% C# goodness

                  **

                  B Offline
                  B Offline
                  bobo jede
                  wrote on last edited by
                  #8

                  Perhaps I understand what you mean, but this doesn't "disable" calling SelectedIndexChanged event. It seems that it is not possible to do, what I want and that it is not correct in OOP principles. Bobo

                  1 Reply Last reply
                  0
                  • B bobo jede

                    You mean Obsolete attribute? I've tried it, but it didn't help :( Thanx for your answer Bobo

                    E Offline
                    E Offline
                    Ed Poore
                    wrote on last edited by
                    #9

                    bobo.jede wrote:

                    You mean Obsolete attribute?

                    Yes

                    bobo.jede wrote:

                    I've tried it, but it didn't help

                    I know it won't but it informs other developers that you shouldn't use it. Maybe if you mark it with this and then in the event throw a NotImplementedException. The obsolete gives them the reason why they should not use it. The only (and best way) to do this would be to write your own control (NB you don't have to write it from scratch you could create a wrapper around an existing ComboBox, only exposing the methods / events / properties you want).


                    Formula 1 - Short for "F1 Racing" - named after the standard "help" key in Windows, it's a sport where participants desperately search through software help files trying to find actual documentation. It's tedious and somewhat cruel, most matches ending in a draw as no participant is able to find anything helpful. - Shog9 Ed

                    1 Reply Last reply
                    0
                    • B bobo jede

                      You mean Obsolete attribute? I've tried it, but it didn't help :( Thanx for your answer Bobo

                      A Offline
                      A Offline
                      Andrew Lygin
                      wrote on last edited by
                      #10

                      Make your new event public and use ObsoleteAttribute with two parameters. In this case compiler will return an error when somebody try to use this event.

                      [Obsolete("SelectedIndexChanged is obsolete. Use MyNewEvent instead.", true)]
                      public new event EventHandler SelectedIndexChanged;

                      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