471,318 Members | 3,268 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

delegates and inheritance

Hi
I just had a bit of a surprise, I expected this to work!

public class A
{
}

public class AChild : A
{
}

public class B
{
}

public class BChild : B
{
}

public delegate A TheDelegate(B b);

private AChild Func(BChild b)
{
return null;
}

public void CallDelegate()
{
TheDelegate a= new TheDelegate(Func);
}
Of course it doesn't work because Func uses child classes of the kinds
used in the
delegate.
Why doesn't this work, if I made a delegate like this:

public delegate object TheDelegate(object b);

I'd expect any function that took any class and returned any class to
work.

However, thats the state of affairs so any way of doing what I want to
do here? i.e. call
Func later?

Ta
C

Feb 10 '06 #1
9 9253
co**********@googlemail.com wrote in news:1139605191.678511.129340
@g44g2000cwa.googlegroups.com:
public delegate A TheDelegate(B b);

private AChild Func(BChild b)
{
return null;
}

public void CallDelegate()
{
TheDelegate a= new TheDelegate(Func);
}
Of course it doesn't work because Func uses child classes of the kinds
used in the
delegate.
Why doesn't this work, if I made a delegate like this:

public delegate object TheDelegate(object b);

I'd expect any function that took any class and returned any class to
work.


Yeah it would work, if the delegate and the function declaration were the
same... If you change your Func(...) so that it accepts a B and returns an
A then it will work fine, even if it returns an AChild - you'll just have
to cast the result.

-mdb
Feb 10 '06 #2
Not an option for me, I've got a hundred or so of these methods.
Wouldn't a C style function pointer have worked in this case?

Feb 10 '06 #3
Yes, but it would have not been type safe.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<co**********@googlemail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Not an option for me, I've got a hundred or so of these methods.
Wouldn't a C style function pointer have worked in this case?

Feb 10 '06 #4
I could have lived with that :-) Nice thing about c++ is that when you
needed to get down and dirty it allowed you to. Of course
it allowed you to screw up as well...
So, no way around this then. I can't change the methods, their in
(effectively) a third party dll. The only way I can think of
doing this is via reflecton at the moment

Feb 10 '06 #5
Say you did this:

public class BChild2 : B
{}

And you were allowed to do this:

TheDelegate d = new TheDelegate(Func);

Now, if you did this:

BChild2 b2 = new BChild2();
d(b2);

You would get an exception because BChild2 can not be cast to BChild,
which is what Func expects. You can pass it to the delegate, because it
expects B, but you can't do it the other way around.

In C# 2.0, you can actually do the reverse. Say your function was this:

Func(B b)

Then, you can say:

TheDelegate d = new TheDelegate(Func);

And that would work, because BChild and BChild2 derive from B.

You might also want to consider declaring your delegate as generic, like
this:

public delegate A TheDelegate<T>(T b) where T : B

This way, you can specify T, but it has to derive from B.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
<co**********@googlemail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hi
I just had a bit of a surprise, I expected this to work!

public class A
{
}

public class AChild : A
{
}

public class B
{
}

public class BChild : B
{
}

public delegate A TheDelegate(B b);

private AChild Func(BChild b)
{
return null;
}

public void CallDelegate()
{
TheDelegate a= new TheDelegate(Func);
}
Of course it doesn't work because Func uses child classes of the kinds
used in the
delegate.
Why doesn't this work, if I made a delegate like this:

public delegate object TheDelegate(object b);

I'd expect any function that took any class and returned any class to
work.

However, thats the state of affairs so any way of doing what I want to
do here? i.e. call
Func later?

Ta
C

Feb 10 '06 #6
<co**********@googlemail.com> wrote:

<snip>
Of course it doesn't work because Func uses child classes of the kinds
used in the
delegate.
Why doesn't this work, if I made a delegate like this:

public delegate object TheDelegate(object b);

I'd expect any function that took any class and returned any class to
work.


The return value "should" work - the parameter "shouldn't", because
someone with an instance of TheDelegate could call it with a parameter
of new object(). If the implementing method is expecting a string, it's
stuffed at that point. However, the reverse "should" work - if the
method only expects an object parameter, it's okay if the delegate is
more specific.

I've put "should" in quotes because this is the behaviour which is
provided by C# 2.0, but not by C# 1.1. It's return value covariance and
parameter contra-variance, and it only works with delegates (not, for
instance, with interfaces).

See http://www.pobox.com/~skeet/csharp/c...delegates.html
It's incomplete, but the bit you're interested in is there.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 10 '06 #7
"Hope this helps"
Well, it probably will do when I get to work on that version so thanks,
reflection for now and templates (I wish), sorry generics later

Active newsgroup this isn't it :-)

Feb 10 '06 #8
<co**********@googlemail.com> wrote:
I could have lived with that :-) Nice thing about c++ is that when you
needed to get down and dirty it allowed you to. Of course
it allowed you to screw up as well...
So, no way around this then. I can't change the methods, their in
(effectively) a third party dll. The only way I can think of
doing this is via reflecton at the moment


You could always wrap the real delegate in your own delegate, calling
the real one and casting appropriately. Ugly, but it would work. (If
you're using C# 2.0 it makes things a bit easier for the reasons I
mentioned before.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 10 '06 #9
Good damn but replies are fast here, thanks all

Feb 10 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Jeffrey T. Smith | last post: by
7 posts views Thread by Brian Henry | last post: by
4 posts views Thread by Tim | last post: by
6 posts views Thread by =?Utf-8?B?Sko=?= | last post: by
4 posts views Thread by Martijn Mulder | last post: by
12 posts views Thread by tshad | last post: by
4 posts views Thread by samadams_2006 | last post: by
reply views Thread by rosydwin | last post: by

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.