why accessor methods?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason Shohet

    #1

    why accessor methods?

    I have a class with protected variables and some accessor methods, , get,
    set ...
    Maybe I have a brain blockage today but I'm thinking, why not just make
    those variables public.
    After all, someone can do just as great harm by misusing a set { }
    accessor method as just doing myObj.lastname = "Big Bird"; which he
    could do in a second class, if the variable is public on the first class.
    IOTW, what does keeping with the protected / accessor methods do for me.

    Thanks
    Jason Shohet



  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: why accessor methods?

    Jason,

    The idea behind properties is that you can perform some extra work when
    a property is set/get, which you can not do with a field. It will allow you
    to validate values when they are set (if you wish), or perform any other
    operation.

    Having the set/get accessors protected allows you to expose the property
    to derived classes and will still allow you to have code that executes when
    a property is set by the subclassed class. Just because a class derives
    from you doesn't mean that you should give it unfettered access to you.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    " Jason Shohet" <ash477@hotmail .com> wrote in message
    news:OiomFIioDH A.1284@TK2MSFTN GP09.phx.gbl...[color=blue]
    > I have a class with protected variables and some accessor methods, , get,
    > set ...
    > Maybe I have a brain blockage today but I'm thinking, why not just make
    > those variables public.
    > After all, someone can do just as great harm by misusing a set { }
    > accessor method as just doing myObj.lastname = "Big Bird"; which he
    > could do in a second class, if the variable is public on the first class.
    > IOTW, what does keeping with the protected / accessor methods do for me.
    >
    > Thanks
    > Jason Shohet
    >
    >
    >[/color]


    Comment

    • Fred Mellender

      #3
      Re: why accessor methods?

      I think one of the purposes of an accessor is to provide the client with a
      convenient interface (looks like assignment to/from a variable), while
      keeping the option of implementing the accessor with a more complicated
      function than just setting or returning a variable. Should the definition
      of the accessor become more complex and need to be changed you can do so
      without impacting the interface you have provided to the client. A simple
      example would be the re-implementation of a "temperatur e" which might be
      kept internally in degrees Kelvin, but returned/set in Centigrade.


      " Jason Shohet" <ash477@hotmail .com> wrote in message
      news:OiomFIioDH A.1284@TK2MSFTN GP09.phx.gbl...[color=blue]
      > I have a class with protected variables and some accessor methods, , get,
      > set ...
      > Maybe I have a brain blockage today but I'm thinking, why not just make
      > those variables public.
      > After all, someone can do just as great harm by misusing a set { }
      > accessor method as just doing myObj.lastname = "Big Bird"; which he
      > could do in a second class, if the variable is public on the first class.
      > IOTW, what does keeping with the protected / accessor methods do for me.
      >
      > Thanks
      > Jason Shohet
      >
      >
      >[/color]


      Comment

      • news.microsoft.com

        #4
        Re: why accessor methods?

        Its also useful for DLL Interop for wrapping and you can perform checks in
        the property accessors and you can also use collections easier in a class.

        get { return _collectionObj[current]; }

        wana try that in a field?

        or

        get { QueryPerformanc eCounter(ref i); return i; }


        wana try that :D


        Methods are for operations like Clear() or Reset() and props are for like
        Count and CurrentItem etc..



        "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
        message news:OqK08LioDH A.2676@TK2MSFTN GP11.phx.gbl...[color=blue]
        > Jason,
        >
        > The idea behind properties is that you can perform some extra work[/color]
        when[color=blue]
        > a property is set/get, which you can not do with a field. It will allow[/color]
        you[color=blue]
        > to validate values when they are set (if you wish), or perform any other
        > operation.
        >
        > Having the set/get accessors protected allows you to expose the[/color]
        property[color=blue]
        > to derived classes and will still allow you to have code that executes[/color]
        when[color=blue]
        > a property is set by the subclassed class. Just because a class derives
        > from you doesn't mean that you should give it unfettered access to you.
        >
        > Hope this helps.
        >
        >
        > --
        > - Nicholas Paldino [.NET/C# MVP]
        > - mvp@spam.guard. caspershouse.co m
        >
        > " Jason Shohet" <ash477@hotmail .com> wrote in message
        > news:OiomFIioDH A.1284@TK2MSFTN GP09.phx.gbl...[color=green]
        > > I have a class with protected variables and some accessor methods, ,[/color][/color]
        get,[color=blue][color=green]
        > > set ...
        > > Maybe I have a brain blockage today but I'm thinking, why not just make
        > > those variables public.
        > > After all, someone can do just as great harm by misusing a set { }
        > > accessor method as just doing myObj.lastname = "Big Bird"; which he
        > > could do in a second class, if the variable is public on the first[/color][/color]
        class.[color=blue][color=green]
        > > IOTW, what does keeping with the protected / accessor methods do for[/color][/color]
        me.[color=blue][color=green]
        > >
        > > Thanks
        > > Jason Shohet
        > >
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Jason Shohet

          #5
          Re: why accessor methods?

          Thanks Nicholas, you and Fred both gave good similar answers. Seems
          accessors vs public properties in the class has nothing to do really with
          scope, but really with not having to redo implementation if a bit of
          business logic changes. I can do that business logic in my get or my set.
          A second question:

          if I do the get / set accessors in classA. Then classB inherits from
          classA. If a client is using classB, can he call the accessors which were
          defined in classA? Or do they need to be re-declared in every class that
          inherits off the base classA?

          Thanks
          Jason Shohet


          Comment

          • Jason Shohet

            #6
            Re: why accessor methods?

            I just usually make member variables protected instead of private if I am
            using accessors. For some reason if they are private, the accessors don't
            work ! :) So I always keep them on protected...


            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: why accessor methods?

              Jason Shohet <ash477@hotmail .com> wrote:[color=blue]
              > I just usually make member variables protected instead of private if I am
              > using accessors. For some reason if they are private, the accessors don't
              > work ! :) So I always keep them on protected...[/color]

              Could you post an example of how they don't work? It's really a good
              idea (IMO) to keep *all* non-constants private, and it certainly *can*
              work, so it would be good to fix whatever's been going wrong in your
              code before.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              Working...