On Thu, 28 Jun 2007 09:56:39 -0700, Jamie Risk <risk.#.@intectus.com> =
[quoted text, click to view] wrote:
> I'm using a class in my project and I'd like to determine if the any o=
f =
[quoted text, click to view] > the fields in the object have been modified since the original =
> instantiation. For intent and purposes, the following class is typica=
l:
I'm not sure what "best practices" has to do with anything here. There =
=
are a variety of mechanisms by which the information you seek could be =
obtained, but what's the "best" way surely depends on how you want it to=
=
work, and what sort of access you have to the classes.
Assuming that semantically what you want to know is whether a mutable =
class is "dirty" (that is, has been instantiated with some specific =
values, at least one of which has subsequently been changed), I would ju=
st =
include a boolean flag in the class to indicate that state. In any of =
your properties, have the setter also set the flag to true. Then you =
could have a read-only property that retrieves the flag.
For example:
[quoted text, click to view] > public class patient
> {
> private string name;
> private string surname;
>
> private bool dirty;
>
> public Name {
> get { return this.name; }
> set { this.name =3D value; dirty =3D true; }
> }
> public Surname {
> get { return this.surname; }
> set { this.surname =3D value; dirty =3D true}
> }
> public Dirty {
> get { return dirty; }
> }
> }
However, obviously you could accomplish this in a variety of other ways.=
=
For example, make the object cloneable and equatable, and then create a =
=
clone immediately after instantiation which you can then later compare f=
or =
equality. Or you could create an event (per-instance) or events =
(per-property) that are raised whenever a property is modified; =
subscribing to those events would tell you as it happens whether changes=
=
have occurred. Or you could simply create a different data structure th=
at =
has copies of the properties in question, which you then compare =
explicitly at a later point in time. Or, some mechanism I haven't liste=
d =
yet could be used instead of any of these.
Any or all of these are appropriate, depending on your needs and what th=
e =
current architecture allows.