.net - C# either return false or do nothing -


i wondering if there's way not have repeat same if construction rather call statuscheck(). can't return true when succeeds. anyone knows better title question?

bool enable() {     if (!getstatus(ref status)) { trace.writeline("error"); return false; }     // stuff      if (!getstatus(ref status)) { trace.writeline("error"); return false; }     // more stuff      if (!getstatus(ref status)) { trace.writeline("error"); return false; }     // more stuff      // 6 more times above      return true; } 

you can create checkstatus() method throws exception if status not valid, handle exception in enable() method:

public void checkstatus(int status) {     if (!isvalidstatus(status)) {         throw new invalidstatusexception(status);     } }  public bool enable() {     try {         checkstatus(status);         // stuff          checkstatus(status);         // more stuff          checkstatus(status);         // more stuff          // 6 more times above          return true;      } catch (invalidstatusexception) {         trace.writeline("error");         return false;     } } 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -