c# - Best practise AddHandler & RemoveHandler or Public boolean variable -


i asked question few months ago. had subroutine selectedindexchanged handler calling changing selected index of control.

i told of 2 ways solve problem, public boolean, or adding , removing event handlers during runtime. starting find more cases of type of problem. best practise solve them?

a public boolean seems simple cluttered fix end several public booleans relating whether different subroutines should run. adding , removing handlers seems cleaner, feels asking compiler lot of work.

you use static boolean within event handler. here short example using textbox.textchanged event handler.

private sub textbox1_textchanged(sender object, _                                  e eventargs) handles textbox1.textchanged     debug.writeline("1")     static reentrant boolean = false     if not reentrant         debug.writeline("2")         reentrant = true         textbox1.text = textbox1.text.toupper         reentrant = false     else         debug.writeline("3")     end if     debug.writeline("4") end sub 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -