"Overloading" delegate

  • Thread starter Thread starter Paul Selormey
  • Start date Start date
P

Paul Selormey

Hello,
What is the best way to have something like this?

protected delegate string CalculationDelegate(int count);
protected delegate string CalculationDelegate(double count);

Best regards,
Paul.
 
Could you just have one delegate that uses a double since an integer can be
cast into a double with no loss of precision?
 
Thanks for the response, and the nice thought.
The posted code was just for illustration, it is not
the real thing I wish to do.

Best regards,
Paul.
 
I am assuming you are coding in C#.

If you want to avoid explicit overloading of the delegate than your
parameter of one type has to implicitly or explicitly convert to the type of
the delegate parameter.
If the parameter is a built-in then try using Convert. for explicit
conversion.
If the parameter objects are yours, then they have to be in an inheritance
relationship (the parameter will be the superclass) or implement the same
interface (the parameter will be the interface). Inside the implementation
of the handler you can query the objects type ( obj is typeof(xxx) ) and on
true cast the parameter to the correct type (xxx) and do your activities.
Do this for each possible type and throw an exception if there isn't a match
('else throw new NotImplementedException(...)' ) . (This will keep your
code honest).

This worked for me.

Chuck
 
Back
Top