I am looking for some help figuring out the best way to refactor these
two examples. I was thinking of using delegates and/or generics; but
have little experience with either. Any sample code would greatly be
appreciated.
I have many methods like, MyReadExternalTempOnly() and MyReset() that
are all very similar and only vary on what method is used as an input
parameter for the IntToError() method. In my first examples, the
ReadExternalTempOnly() and Reset() methods are used. My second
examples only differ in the method being called, GetTemp() and
GetDewPT().
Thanks in advance,
Randel
/
************************************************** *******************/
First Example:
//---------------------------------------------------------------------
public static THUM_ERROR MyReset(){
THUM_ERROR Status = THUM_ERROR.WRITE_FAILED;
if(Installed){
MyRead();
Status = IntToError(Reset());
if(Status != THUM_ERROR.SUCCESS){
throw new THUMError(Status);
}//END "" IF-STATEMENT
}//END "" IF-STATEMENT
else{
throw new THUMError(THUM_ERROR.THUM_NOT_INSTALLED);
}//END ELSE
return(Status);
}
//---------------------------------------------------------------------
public static THUM_ERROR MyReadExternalTempOnly(){
THUM_ERROR Status = THUM_ERROR.WRITE_FAILED;
if(Installed){
MyRead();
Status = IntToError(ReadExternalTempOnly());
if(Status != THUM_ERROR.SUCCESS){
throw new THUMError(Status);
}//END "" IF-STATEMENT
}//END "" IF-STATEMENT
else{
throw new THUMError(THUM_ERROR.THUM_NOT_INSTALLED);
}//END ELSE
return(Status);
}
First Example Solution (something like this):
//---------------------------------------------------------------------
public static THUM_ERROR MyReadExternalTempOnly(){
return(DELEGATE_METHOD(ReadExternalTempOnly()));
}
//---------------------------------------------------------------------
public static THUM_ERROR MyReset(){
return(DELEGATE_METHOD(Reset()));
}
/
************************************************** *******************/
Second Example:
//---------------------------------------------------------------------
public static double MyGetTemperature(){
double ReturnValue = 0.0;
if(MyRead() == THUM_ERROR.SUCCESS){
ReturnValue = GetTemp();
}//END "" IF-STATEMENT
return(ReturnValue);
}
//---------------------------------------------------------------------
public static double MyGetDewPoint(){
double ReturnValue = 0.0;
if(MyRead() == THUM_ERROR.SUCCESS){
ReturnValue = GetDewPT();
}//END "" IF-STATEMENT
return(ReturnValue);
}
Second Example Solution (something like this):
//---------------------------------------------------------------------
public static double MyGetTemperature(){
return(DELEGATE_METHOD(GetTemp()));
}
//---------------------------------------------------------------------
public static double MyGetDewPoint(){
return(DELEGATE_METHOD(GetDewPT()));
}