Help with delegates and/or generics

R

rbjorkquist

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()));
}
 
N

Nicholas Paldino [.NET/C# MVP]

Randel,

For the first example, it seems like you really want a delegate like so
(I'm assuming that THUM_ERROR is an enumeration value):

delegate THUM_ERROR ThumMethod();

And then a method like this:

public static THUM_ERROR ExecuteThumMethod(ThumMethod method)
{
if(Installed)
{
MyRead();

Status = IntToError(method());

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 THUM_ERROR.WRITE_FAILED;
}

Which you then call like this:

public static THUM_ERROR MyReadExternalTempOnly()
{
return ExecuteThumMethod(ReadExternalTempOnly);
}

On a side note, it looks like you are combining error codes with
exceptions which is not a good idea. You might want to reconsider this, as
it looks like you don't know where the errors in input vs. exceptional cases
are.

For the second example, it's the same thing:

delegate double Calculation();

public static double ExecuteCalculation(Calculation calculation)
{

if(MyRead() == THUM_ERROR.SUCCESS)
{
return calculation();
}

// Return 0.
return 0;
}

Which you then call like this:

public static double MyGetTemperature()
{
return ExecuteCalculation(GetTemp);
}
 
R

rbjorkquist

Hey Nicholas,

Thanks for the response. You are correct that THUM_ERROR is an enum.
Sorry if I didn't give enough information. I'm attempting to write a
wrapper around a third party driver. This driver, when attempting to
set a state or read a sensor, returns an integer value. So I created
an enum and an associated method that converts the return values.

I was contemplating throwing an exception when an error occurred
because it's an external device and I have no way of knowing when the
device is attached until I attempt a read of some kind. It's this you
are talking about when you saying "...it looks like you are combining
error codes with exceptions...", right? Also, I don't understand your
comment, "...it looks like you don't know where the errors in input
vs. exceptional cases are". Are you saying to chose either a return
value, in this case of the enum type, or through an exception? If so,
is there a guideline you use?

Thanks again for the help,

Randel
 
N

Nicholas Paldino [.NET/C# MVP]

Randel,

Well, I don't really know what the values in the THUM_ERROR enumeration
are. If you don't get a success code, you are throwing an exception.
However (and I don't know the model here), if there is an error that is due
to something non exceptional (like if you read from the device and it gives
you something indicating that you issued an incorrect request, or something
like that), then you should be returning an error code. Otherwise, I would
say throw an exception.

It's very possible that you are doing that here, but I can't tell
without knowing what the overall design is, or the other values from the
enumeration, for that matter. The general guideline I can give is that if
what occurs is an error (e.g. incorrect user input is the most common
example, while it might not be the case here), then I would return a code.
Otherwise, if it is an exceptional case that occurs outside of what is
expected (e.g. hardware failure) then throw an exception.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top