1

I'd like to have it so that there is a function that is called anytime a variable inside my class is changed. I know I could do this with get-set variables but if I'd prefer not to set that up for every single variable in my class is there a better way?

Coat
  • 673
  • 6
  • 17
  • @DanielAbouChleih: Not really a duplicate, as the accepted solution of that question was explicitly ruled out here (not that there would be any other way to go). – O. R. Mapper Dec 16 '13 at 10:16
  • Standard issue "help me circumvent the language" question. Just because the question rules out the solution, does _not_ mean that the restriction is reasonable or applicable . – Gusdor Dec 16 '13 at 10:18
  • @Gusdor: As evidenced by the existence of two answers, the restriction is very much applicable (to the question). – O. R. Mapper Dec 16 '13 at 10:39
  • @O.R.Mapper My use of 'applicable' referred to the absence of that language feature. Sorry for confusion. Both answers _correctly_ disregard the requirement. – Gusdor Dec 16 '13 at 10:45

2 Answers2

3

No there's no better way. You can use tools like PostSharp to make it easier for you, but you'll need properties.

Daniel Häfele
  • 852
  • 2
  • 8
  • 13
2

There is no such mechanism available. Writing to an instance variable is pretty much like directly writing some bytes at a given memory address, and there is no hook notifying you when that is done. That is exactly what properties with their getters and setters are there for.

O. R. Mapper
  • 18,591
  • 9
  • 58
  • 102
  • 1
    To the OP, it is worth using visual studio _code snippets_ to do a lot of this code for you. It can be time consuming without. – Gusdor Dec 16 '13 at 10:46