I wondering if there’s a way to not have to repeat the same if construction but rather call a StatusCheck(). It can’t return true when it succeeds. Anyone knows a better title for this question?
bool Enable()
{
if (!GetStatus(ref status)) { Trace.WriteLine("Error"); return false; }
// do stuff
if (!GetStatus(ref status)) { Trace.WriteLine("Error"); return false; }
// do more stuff
if (!GetStatus(ref status)) { Trace.WriteLine("Error"); return false; }
// do even more stuff
// 6 more times the above
return true;
}
You can create a
CheckStatus()method that throws an exception if the status is not valid, then handle that exception in yourEnable()method: