c# - Is there an easier way of writing several if/else statements into eachother? -
i have myself writing code requires several layers of if/else statements incorporated each other (example can found below).
wondering if shorten bit, because have trees of if/else statements of on 70 lines of code, , filling way compared how many of lines seem redundant.
here's example code:
if (labelgivetip1.visible == true) { if (labelgivetip2.visible == true) { labelgivetip3.visible = true; if (labelgivetip3.visible == true) { custom_dialogbox.show("all of hints assignment used, want annother assignmment?", //main text argument "error: no more hints", //header argument "back", //first button text argument "get new assignment"); //second button text argument //this custom dialog box result = custom_dialogbox.result; switch (result) { case dialogresult.yes: buttonnextassignment.performclick(); break; case dialogresult.cancel: break; default: break; } } } else { labelgivetip2.visible = true; } } else { labelgivetip1.visible = true; }
in code tend check false condition first , return asap. approach has helped me on years reduce nested if else. other try separate related functionalities in different methods. example method provided crams of logic 1 method. if have resharper, suggests nice improvements , on period of time, becomes habit.
Comments
Post a Comment