"Overloading" delegate

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.
 
P

Peter Rilling

Could you just have one delegate that uses a double since an integer can be
cast into a double with no loss of precision?
 
P

Paul Selormey

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.
 
C

Chuck

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
 

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