J
Jack
Hi,
I have a class with a get-only public bool property. This is good because I
want users of this class to ONLY READ it.
So I've defined it thus:
private bool _IsDirty;
public bool IsDirty{
get{ return _IsDirty; }
}
.... no problems there.
But the methods from WITHIN this class needs to write to it, but the
compiler will not allow me. So I guess I have 2 choices:
1. Put a set{ _IsDirty = value; } clause. This will allow the methods from
WITHIN this class to write to it (i.e. IsDirty = true
, but it defeats the
purpose because the users of this class are now ALSO able to modify it -
THIS IS BAD!.
2. When writing to it, write to the private value (i.e. _IsDirty = true; ).
This is my preferred choice at the moment, but it seems to defeat the
purpose of encapsulation and good programming practice; because it the
implementation of IsDirty is to be changed in the future, I'll have to step
through all the code where I reference it.
What is the preferred way, or is there another approach? have I missed
something?
Thanks,
jack.
I have a class with a get-only public bool property. This is good because I
want users of this class to ONLY READ it.
So I've defined it thus:
private bool _IsDirty;
public bool IsDirty{
get{ return _IsDirty; }
}
.... no problems there.
But the methods from WITHIN this class needs to write to it, but the
compiler will not allow me. So I guess I have 2 choices:
1. Put a set{ _IsDirty = value; } clause. This will allow the methods from
WITHIN this class to write to it (i.e. IsDirty = true

purpose because the users of this class are now ALSO able to modify it -
THIS IS BAD!.
2. When writing to it, write to the private value (i.e. _IsDirty = true; ).
This is my preferred choice at the moment, but it seems to defeat the
purpose of encapsulation and good programming practice; because it the
implementation of IsDirty is to be changed in the future, I'll have to step
through all the code where I reference it.
What is the preferred way, or is there another approach? have I missed
something?
Thanks,
jack.