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. member hiding question

member hiding question

Scheduled Pinned Locked Moved C#
questionhelp
15 Posts 3 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.
  • L Offline
    L Offline
    likefood
    wrote on last edited by
    #1

    I've looked in some CP articles and in my MSDN library, but can't find the answer to my question... How do I hide a public member of a base class? I'm trying to inherit from the TrackBar class, and I want to make the public members Value and Maximum inaccessible (make them private members instead of public). I've tried just saying: private new int Value; and private new int Maximum; in my inheriting class, but, elsewhere in the code, when I have: myTrackBar.Maximum [...] it doesn't give me a compile error like I want. So... How do I, like, convert the public members Value and Maximum into private members?

    -Daniel Typing too fast fro my owngood

    C 1 Reply Last reply
    0
    • L likefood

      I've looked in some CP articles and in my MSDN library, but can't find the answer to my question... How do I hide a public member of a base class? I'm trying to inherit from the TrackBar class, and I want to make the public members Value and Maximum inaccessible (make them private members instead of public). I've tried just saying: private new int Value; and private new int Maximum; in my inheriting class, but, elsewhere in the code, when I have: myTrackBar.Maximum [...] it doesn't give me a compile error like I want. So... How do I, like, convert the public members Value and Maximum into private members?

      -Daniel Typing too fast fro my owngood

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      You totally cannot change your base class. You could make an intermediate class, which makes those properties private to THAT class, and do your real work in a class derived from that, but what a tangled web you want to weave. Why do you want to make things private to a class you don't have access to ?

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      L 1 Reply Last reply
      0
      • C Christian Graus

        You totally cannot change your base class. You could make an intermediate class, which makes those properties private to THAT class, and do your real work in a class derived from that, but what a tangled web you want to weave. Why do you want to make things private to a class you don't have access to ?

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

        L Offline
        L Offline
        likefood
        wrote on last edited by
        #3

        I'm trying to make a visually reversed trackbar. For example, pretend the trackbar's value is 100 (out of 300). The public member "rValue" would tell you that the value is actually 200 (meaning Maximum - (Value - Minimum)). And, if the user set "rMaximum" to a certain value, it would adjust the Value accordingly (to keep it the same amount below Maximum, not the same amount above Minimum).

        -Daniel Typing too fast fro my owngood

        C 1 Reply Last reply
        0
        • L likefood

          I'm trying to make a visually reversed trackbar. For example, pretend the trackbar's value is 100 (out of 300). The public member "rValue" would tell you that the value is actually 200 (meaning Maximum - (Value - Minimum)). And, if the user set "rMaximum" to a certain value, it would adjust the Value accordingly (to keep it the same amount below Maximum, not the same amount above Minimum).

          -Daniel Typing too fast fro my owngood

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          OK. Well, why not override the Value method instead and have it track in reverse ?

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

          L 1 Reply Last reply
          0
          • C Christian Graus

            OK. Well, why not override the Value method instead and have it track in reverse ?

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

            L Offline
            L Offline
            likefood
            wrote on last edited by
            #5

            Two responses... 1) Value is a method? I thought it was just an int member... I suppose it would be most efficient to make it one of those funky methods that look like members to outsiders, so that whenever it was changed the little tick thing would move... 2) If I try to override it, I can just call ((TrackBar)base).Value from inside my class, right? I'll give it a shot, thanks!

            -Daniel Typing too fast fro my owngood

            C 1 Reply Last reply
            0
            • L likefood

              Two responses... 1) Value is a method? I thought it was just an int member... I suppose it would be most efficient to make it one of those funky methods that look like members to outsiders, so that whenever it was changed the little tick thing would move... 2) If I try to override it, I can just call ((TrackBar)base).Value from inside my class, right? I'll give it a shot, thanks!

              -Daniel Typing too fast fro my owngood

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              1 - OK, I mixed my terminology. I doubt it's an int. I meant it's a property. 2 - Yes, if you use new and not override, I'd expect you can do that.

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

              L 1 Reply Last reply
              0
              • C Christian Graus

                1 - OK, I mixed my terminology. I doubt it's an int. I meant it's a property. 2 - Yes, if you use new and not override, I'd expect you can do that.

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

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

                Well, this is what I tried (code below). However, the compiler says "Use of keyword 'base' is not valid in this context". I thought that base was like this, but for accessing a parent class. How should I do it?

                public class ReverseTrackBar : TrackBar
                {
                private new int Value
                {
                get { return (((TrackBar)base).Maximum - (((TrackBar)base).Value - this.Minimum)); }
                set { ((TrackBar)base).Value = (((TrackBar)base).Maximum - ((int)value - this.Minimum)); }
                }
                private new int Maximum
                {
                get { return ((TrackBar)base).Maximum; }
                set
                {
                int val = this.Value;
                ((TrackBar)base).Maximum = (int)value;
                this.Value = val;
                }
                }
                public ReverseTrackBar()
                {
                this.Value = 0;
                }
                }

                -Daniel Typing too fast fro my owngood

                C L J 4 Replies Last reply
                0
                • L likefood

                  Well, this is what I tried (code below). However, the compiler says "Use of keyword 'base' is not valid in this context". I thought that base was like this, but for accessing a parent class. How should I do it?

                  public class ReverseTrackBar : TrackBar
                  {
                  private new int Value
                  {
                  get { return (((TrackBar)base).Maximum - (((TrackBar)base).Value - this.Minimum)); }
                  set { ((TrackBar)base).Value = (((TrackBar)base).Maximum - ((int)value - this.Minimum)); }
                  }
                  private new int Maximum
                  {
                  get { return ((TrackBar)base).Maximum; }
                  set
                  {
                  int val = this.Value;
                  ((TrackBar)base).Maximum = (int)value;
                  this.Value = val;
                  }
                  }
                  public ReverseTrackBar()
                  {
                  this.Value = 0;
                  }
                  }

                  -Daniel Typing too fast fro my owngood

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  I guess the 'new' keyword is hiding the base value.

                  Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                  L 1 Reply Last reply
                  0
                  • L likefood

                    Well, this is what I tried (code below). However, the compiler says "Use of keyword 'base' is not valid in this context". I thought that base was like this, but for accessing a parent class. How should I do it?

                    public class ReverseTrackBar : TrackBar
                    {
                    private new int Value
                    {
                    get { return (((TrackBar)base).Maximum - (((TrackBar)base).Value - this.Minimum)); }
                    set { ((TrackBar)base).Value = (((TrackBar)base).Maximum - ((int)value - this.Minimum)); }
                    }
                    private new int Maximum
                    {
                    get { return ((TrackBar)base).Maximum; }
                    set
                    {
                    int val = this.Value;
                    ((TrackBar)base).Maximum = (int)value;
                    this.Value = val;
                    }
                    }
                    public ReverseTrackBar()
                    {
                    this.Value = 0;
                    }
                    }

                    -Daniel Typing too fast fro my owngood

                    L Offline
                    L Offline
                    likefood
                    wrote on last edited by
                    #9

                    Oh, I guess you don't have to cast base to a type because the type is already known (the type I'm inheriting from).

                    -Daniel Typing too fast fro my owngood

                    C 1 Reply Last reply
                    0
                    • C Christian Graus

                      I guess the 'new' keyword is hiding the base value.

                      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                      L Offline
                      L Offline
                      likefood
                      wrote on last edited by
                      #10

                      No, that's not it. See the message I posted at the same time as you...

                      -Daniel Typing too fast fro my owngood

                      1 Reply Last reply
                      0
                      • L likefood

                        Oh, I guess you don't have to cast base to a type because the type is already known (the type I'm inheriting from).

                        -Daniel Typing too fast fro my owngood

                        C Offline
                        C Offline
                        Christian Graus
                        wrote on last edited by
                        #11

                        Yes, I wondered about that :-)

                        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                        1 Reply Last reply
                        0
                        • L likefood

                          Well, this is what I tried (code below). However, the compiler says "Use of keyword 'base' is not valid in this context". I thought that base was like this, but for accessing a parent class. How should I do it?

                          public class ReverseTrackBar : TrackBar
                          {
                          private new int Value
                          {
                          get { return (((TrackBar)base).Maximum - (((TrackBar)base).Value - this.Minimum)); }
                          set { ((TrackBar)base).Value = (((TrackBar)base).Maximum - ((int)value - this.Minimum)); }
                          }
                          private new int Maximum
                          {
                          get { return ((TrackBar)base).Maximum; }
                          set
                          {
                          int val = this.Value;
                          ((TrackBar)base).Maximum = (int)value;
                          this.Value = val;
                          }
                          }
                          public ReverseTrackBar()
                          {
                          this.Value = 0;
                          }
                          }

                          -Daniel Typing too fast fro my owngood

                          J Offline
                          J Offline
                          jjansen
                          wrote on last edited by
                          #12

                          Heritos Gger wrote:

                          However, the compiler says "Use of keyword 'base' is not valid in this context".

                          The error is not related to the use of the keyword 'base', but to the fact that you try to cast the base to type TrackBar. You don't need to do that. Remove all of these casts (e.g. ((TrackBar)base).Value should be base.Value) and the compiler error will disappear.

                          L 1 Reply Last reply
                          0
                          • L likefood

                            Well, this is what I tried (code below). However, the compiler says "Use of keyword 'base' is not valid in this context". I thought that base was like this, but for accessing a parent class. How should I do it?

                            public class ReverseTrackBar : TrackBar
                            {
                            private new int Value
                            {
                            get { return (((TrackBar)base).Maximum - (((TrackBar)base).Value - this.Minimum)); }
                            set { ((TrackBar)base).Value = (((TrackBar)base).Maximum - ((int)value - this.Minimum)); }
                            }
                            private new int Maximum
                            {
                            get { return ((TrackBar)base).Maximum; }
                            set
                            {
                            int val = this.Value;
                            ((TrackBar)base).Maximum = (int)value;
                            this.Value = val;
                            }
                            }
                            public ReverseTrackBar()
                            {
                            this.Value = 0;
                            }
                            }

                            -Daniel Typing too fast fro my owngood

                            J Offline
                            J Offline
                            jjansen
                            wrote on last edited by
                            #13

                            I think the solution to your problem is this: Hide the property of the base class, but make the access modifier the same as in the base class, i.e. public. Next, add the modifier protected to the set-accessor (.NET 2.0 required). The get-accessor cannot be protected as well, but I don't think that's a problem, I fact I think it's good practice to keep the get-accessor accessable. You'll get something like this: public new int Value { get { return (base.Maximum - (base.Value - this.Minimum)); } protected set { base.Value = base.Maximum - (value - this.Minimum); } } That should work.

                            L 1 Reply Last reply
                            0
                            • J jjansen

                              Heritos Gger wrote:

                              However, the compiler says "Use of keyword 'base' is not valid in this context".

                              The error is not related to the use of the keyword 'base', but to the fact that you try to cast the base to type TrackBar. You don't need to do that. Remove all of these casts (e.g. ((TrackBar)base).Value should be base.Value) and the compiler error will disappear.

                              L Offline
                              L Offline
                              likefood
                              wrote on last edited by
                              #14

                              Yes, you're right. I realized that, and now it works beautifully. http://www.codeproject.com/script/comments/forums.asp?forumid=1649&select=1719871&df=100&fr=70.5#xx1719871xx[^]

                              -Daniel Typing too fast fro my owngood

                              1 Reply Last reply
                              0
                              • J jjansen

                                I think the solution to your problem is this: Hide the property of the base class, but make the access modifier the same as in the base class, i.e. public. Next, add the modifier protected to the set-accessor (.NET 2.0 required). The get-accessor cannot be protected as well, but I don't think that's a problem, I fact I think it's good practice to keep the get-accessor accessable. You'll get something like this: public new int Value { get { return (base.Maximum - (base.Value - this.Minimum)); } protected set { base.Value = base.Maximum - (value - this.Minimum); } } That should work.

                                L Offline
                                L Offline
                                likefood
                                wrote on last edited by
                                #15

                                Yes, that's close to what I ended up with. The set accessor is fine to be public because I can perform the reversal of data right there. I just didn't want the "outside" to have access to the base class's Value and Maximum properties. All I needed to do was overload them with new, and now it works beautifully. Thank you, everyone! Case closed.

                                -Daniel Typing too fast fro my owngood

                                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