Results 1 to 5 of 5

Thread: Accessor Methods in .NET

  1. #1
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    350

    Lightbulb Accessor Methods in .NET

    I've been writing an RPG for a while, but started running into problems because I didn't abstract things out correctly. Anyways, I'm rewriting the codebase to be more developer-friendly, but I'm stuck on something.

    What is the point of an accessor method, as opposed to simply having a public member? For example, I have a clsSprite class. The class has a Point object to specify the location of the sprite on the game screen. In the original codebase, I had:
    Code:
    public Point ptLocation = new Point();
    What is the advantage of having this instead?:
    Code:
            private Point ptLocation = new Point();
    
            public int LocationX
            {
                get
                {
                    return ptLocation.X;
                }
                set
                {
                    ptLocation.X = value;
                }
            }
            public int LocationY
            {
                get
                {
                    return ptLocation.Y;
                }
                set
                {
                    ptLocation.Y = value;
                }
            }
    I know it has something to do with memory management, but it doesn't make much sense to me. This seems like there's just more code to bloat my class. Am I wrong?
    Geek isn't just a four-letter word; it's a six-figure income.

  2. #2
    Banned
    Join Date
    Aug 2001
    Location
    Yes
    Posts
    4,424
    One of the advantages of using properties (your accessor in this case) is validation. If you define a new point at an x value of 20000 for example (a position outside your screen, which shouldn't be allowed), you use the set part of the accessor to make sure the values are within range:

    Code:
    set
    {
    if((value >= 0) && (value < 1280))
    pLocation.X = value;
    }
    This makes sure that the x value gets validated before it is assigned. Additionally, it makes the fields private (as they should be), while still providing access (through the accessor).

    I'm sure someone else can explain it better...

  3. #3
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    350
    Well that particular example is a good one, so thanks, but I still wonder why things *should* be private.

    Expert question: When using an accessor method, is that compiled like a function? If I were to write a function with the same purpose (I know, why would I bother?), would the generated asm (I know .NET uses CLR, but you get my point) be more or less the same, or is there something different about accessor methods that make them special?
    Geek isn't just a four-letter word; it's a six-figure income.

  4. #4
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    I know this is a bit late, but here's another reason - making stuff "readonly". Say you have a collection in your class and you want people to be able to access it, but you don't want them to be able to alter the actual reference:

    Code:
    public class MyClass
    {
      private ArrayList myList;
    
      public ArrayList List
      {
        get
        {
          return myList;
        }
      }
    
      public MyClass()
      {
        myList = new ArrayList();
      }
    }
    Not the best example ever, but it means that they can add/remove/etc stuff from the collection, but they can't change which collection myList points to.

    To answer your other question, as far as I know, the C# compiler will take your property and generate a get_MyProperty() method and a set_MyProperty() method. If you download Reflector you can see this by opening an assembly (either a CLR .dll or .exe) and looking for a property then expanding it. It'll also let you view the CIL that's generated for it.

    ac
    Last edited by gothic_type; April 1st, 2008 at 08:04 PM.

  5. #5
    Ninja Code Monkey
    Join Date
    Nov 2001
    Location
    Washington State
    Posts
    1,027
    Using properties gives you the ability to make items read or write only, allows you to do validation of your data (which you should be doing), and can give you opportunities to make your objects thread safe.
    "When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
    "There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
    "Mischief my ass, you are an unethical moron." - chsh
    Blog of X

Similar Threads

  1. Decompiling .net and code obfuscation
    By Juridian in forum The Security Tutorials Forum
    Replies: 3
    Last Post: February 12th, 2006, 04:40 PM
  2. Test your Web services and .NET components
    By avdven in forum Web Development
    Replies: 1
    Last Post: July 24th, 2002, 06:18 AM
  3. Programming in C#
    By Mankan in forum Other Tutorials Forum
    Replies: 8
    Last Post: July 23rd, 2002, 02:43 PM
  4. Microsoft .NET
    By Negative in forum Other Tutorials Forum
    Replies: 13
    Last Post: July 11th, 2002, 10:03 PM
  5. Micro$oft postpones .NET My services
    By jcdux in forum Microsoft Security Discussions
    Replies: 0
    Last Post: April 12th, 2002, 11:23 AM

Posting Permissions

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