473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help with delegates and/or generics

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

Jul 2 '07 #1
3 1325
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);
}
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"rbjorkquist" <rb*********@coilcraft.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
>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()));
}

Jul 2 '07 #2
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

Jul 2 '07 #3
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"rbjorkquist" <rb*********@coilcraft.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
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

Jul 3 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: bigtexan | last post by:
I would like to do the following and cannot figure it out. public class A<T> { public delegate T GetValueDelegate(A<T> var); public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);...
30
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment...
6
by: PJ6 | last post by:
I would like to refer to properties in code without having to resort to using a string for the name. AddessOf gives me this ability for methods, but I can't find a single way to point that at the...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
2
by: RSH | last post by:
I have been looking at delegates lately. I have seen several articles explaining them but I'm having a hard time understanding why I would use a delegate over a traditional function or sub. Could...
5
by: r035198x | last post by:
The delegates white paper say they are not neccessary. I buy most of the arguments there. However it's possible to implement them in java (That article could have been improved by using generics in...
2
by: jehugaleahsa | last post by:
Hello: I was reading another post and saw an argument (sort of) that brought up a good question. I have written a fairly large library of algorithms that occur in programming all the time. It...
10
by: tshad | last post by:
I don't think there are delegates for VB as there is for C#. Why is that and are they planning to add this? This seems to be the only thing that VB doesn't have that C# does. Thanks, Tom
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.