I am trying to write some code in which one class, when it reaches a certain state, tells its parent class to do something:
I don't want to have to keep asking if (A.B.x == 50) after every call to functionB, because x is only equal to 50 after, on average, hundreds of calls to functionB(), so this would be very inefficient. Function B changes x randomly so there is no way of telling when exactly x will equal 50.
B = new classB();
// Modifies the value of x in a random way
A = new classA();
A.B.functionB() /* This is called lots and lots, until x happens to equal 50. When x = 50, I want to dispose of B, and call A.functionA().*/
So, how can I tell A to do something once B reaches a certain state....could I use some sort of event handler, such that x=50 raises and event, thus telling A do dispose of B and so on?
I am very new to C# and would like to know what the code for such an event handler would be, or any other suggestions on how I might implement this.
You have no choice. To react to x being 50, you have to test whether x is 50. Where you do it is irrelevant.