-
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?
-
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...
-
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?
-
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
-
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.