473,236 Members | 1,316 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,236 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 9369
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jeffrey T. Smith | last post by:
Back when the new J2SE1.5 features were announced, there was a JavaLive community chat (http://java.sun.com/developer/community/chat/JavaLive/2003/jl0729.html) in which Neal Gafter explains the...
7
by: Brian Henry | last post by:
What exactly are they? I haven't hit that chapter in my VB.NET book yet, was just wondering if i could get a quick over view? thanks!
8
by: Nicky Smith | last post by:
Hello, I'm reading Mike Gunderloy's Mcad Vb.net book, and I've also read the MS press Mcad book for the same topic ".. Windows based applications with VB.net" for exam 70-306. In the...
4
by: Tim | last post by:
There are a set of clients who need to be notified of certain events. I have used events and delegates (publisher-Subscriber model) for the notification mechanism. All the clients register with...
6
by: =?Utf-8?B?Sko=?= | last post by:
I have a logger component that logs to multiple sources, ie textfile, eventlog etc. and I have two methods that depending on where I call up my logger comp. one of them will be called. For ex. if...
4
by: Martijn Mulder | last post by:
You can fake the mechanism of inheritance by passing in a delegate function to the constructor. The constructor sets the delegate to a certain named function, Greet() in the example below. By...
12
by: tshad | last post by:
I have a set up javascript functions that pass function pointers and I am trying to figure out how to do the same thing in C# using delegates. // We define some simple functions here function...
69
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
4
by: samadams_2006 | last post by:
Hello, I'm trying to figure out why delegates are such a great thing. For example, I came across the following line of code: "This ability to refer to a method as a parameter makes delegates...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.