How to automatically reset a boolean when another method is called in C #?

Using C #, I need to do extra work if the function A() was called immediately before the function C() . If any other function was called between A() and C() , then I do not want to do this extra work. Any ideas that require the least amount of code duplication?

I try to avoid adding lines like flag = false; into every function B1 .. BN .

Here is a very simple example:

 bool flag = false; void A() { flag = true; } void B1() { ... } void B2() { ... } void C() { if (flag) { //do something } } 

The above example was just a simple case, but I am open to using something other than logical. The important thing is that I want to be able to set and reset the sort flag so that C() knows how to behave accordingly.

Thank you for your help. If you need clarification, I will edit my post.

+4
source share
4 answers

I solved the problem with a similar situation (i.e. the need to know if A was called immediately before C), having just a state station. Essentially, I built a state object using enum and a property to control / query the state.

When my equivalent A () was called, it would have to store a piece of business logic from a state indicating that A. had been called. If other methods (your B) were called, it would switch the state to one of several other states ( my situation was a bit more complicated), and then, when C () was called, part of the business logic was requested to determine if they were going to name some method D () that held the functionality "only if A was called".

I suspect there are several ways to solve this problem, but I liked the approach to the state computer that I took because it allowed me to expand the original binary situation to handle a more complex situation with several states.

I was fortunate that multithreading was not a problem in my case, because it tends to make things more interesting, but the state machine will probably work in this scenario as well.

Only my two cents.

+1
source

Why not just split your “extra work” into a memorable function (that is, one that caches its results)? Whenever you need this work, you simply call this function, which will be short if the cache is fresh. Whenever this work becomes obsolete, invalidate the cache. In your rather strange examples above, I assume that you will need a function call in each of B, and one in C. Calling A will invalidate the cache.

If you are looking for this around (i.e. some kind of smart way to catch all function calls and insert this call), I really would not bother. I can imagine some kind of crazy generation of a reflection-time proxy class, but you have to make your code stream clear and obvious; if each function depends on the work already done, just call "doWork" in each of them.

+7
source

It looks like your design is too closely connected if calling one method changes the behavior of another, so you should definitely call them in the correct order. This is a large red flag.

It looks like some refactoring is fine. It's a little difficult to give advice without seeing more of the real code, but here's the point in the right direction.

Consider adding a parameter to C like this:

 void C(bool DoExtraWork) { if (DoExtraWork)... } 

Of course, “DoExtraWork” should be called something meaningful in the context of the caller.

+3
source

I do not recommend this, but what the hell: if you want to replace all your simple method calls:

 A(); 

... with the syntax:

 // _lastAction is a class-level Action member (_lastAction = new Action(A)).Invoke(); 

... then inside C() you can just do the check like this:

 void C() { if (_lastAction.Method.Name == "A") { } } 

This is probably not thread safe (and it will not work with code running through the obfuscator without any intervention), so I will not use something like this without hard testing. I will also not use something like this period.

Note: my ancient C # version has an Action<T> (not an Action or Action<T, T> etc.), so if you are stuck there, you will have to add a dummy parameter for each method to use this approach.

0
source

Source: https://habr.com/ru/post/1306747/


All Articles