473,513 Members | 2,519 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

delegates and virtual functions

Do delegates don't respect polymorphisms -- why?

say in this example:

using System;

public delegate string FirstDelegate (int x);
abstract class Base
{
public abstract string Foo(int );

public void Dispatch()
{
FirstDelegate del=new FirstDelegate (Foo);
del(5);
}
}

class Derived
{
public override string Foo(int x)
{
return " "+x;
}
}
class Runner
{

public static void Main()
{
Derived d=new Derived();
d.Dispatch(); //trys to delegate bases
}
}
The same thing happens when Base is NOT abstract class and Foo is
declared as virtual....

Is there a rule don't mix delegates and runtime polymorphism???

Oct 21 '08 #1
3 5086
What same thing? Unfortunately, you haven't pasted the exact code [it
doesn't compile...], so we can't know what you are seeing, since any
repairs we make may change the problem...

However, making the most obvious fixes (adding a base class, naming
the argument, etc) - it all works fine and the override in Derived is
called.

Delegates respect polymorphism.

Perhaps tell us what you are seeing?

Marc
[C# MVP]
Oct 21 '08 #2
Hi,

why should delegates not respect polymorphisms ?
This code, works as expected for me. FirstDelegate
points to the implementation of Foo(..) in Derived.
The only thing that leads to a StackOverflowException is
doing

class DerivedOverflow : Derived
{
public override string Foo(int x)
{
return "-" + base.Dispatch();
}
}

which is also expected behaviour from my understandings.

Perhaps you could explain a little more what you mean.

Philipp

puzzlecracker schrieb:
Do delegates don't respect polymorphisms -- why?

say in this example:

using System;

public delegate string FirstDelegate (int x);
abstract class Base
{
public abstract string Foo(int );

public void Dispatch()
{
FirstDelegate del=new FirstDelegate (Foo);
del(5);
}
}

class Derived
{
public override string Foo(int x)
{
return " "+x;
}
}
class Runner
{

public static void Main()
{
Derived d=new Derived();
d.Dispatch(); //trys to delegate bases
}
}
The same thing happens when Base is NOT abstract class and Foo is
declared as virtual....

Is there a rule don't mix delegates and runtime polymorphism???
Oct 21 '08 #3
On Oct 21, 10:06*am, Philipp Brune <her...@gmx.dewrote:
Hi,

why should delegates not respect polymorphisms ?
This code, works as expected for me. FirstDelegate
points to the implementation of Foo(..) in Derived.
The only thing that leads to a StackOverflowException is
doing

* class DerivedOverflow : Derived
* * *{
* * * * *public override string Foo(int x)
* * * * *{
* * * * * * *return "-" + base.Dispatch();
* * * * *}
* * *}

which is also expected behaviour from my understandings.

Perhaps you could explain a little more what you mean.

Philipp

puzzlecracker schrieb:
Do delegates don't respect polymorphisms -- why?
say in this example:
using System;
public delegate string FirstDelegate (int x);
abstract class Base
{
*public abstract string *Foo(int );
* * * public void Dispatch()
* * * {
* * * * * * FirstDelegate *del=new FirstDelegate (Foo);
* * * * * * del(5);
* * }
}
class Derived
{
* * *public override string *Foo(int x)
* * * {
* * * * * *return " "+x;
* * *}
}
class Runner
{
* * * * public static void Main()
* * * * {
* * * * * * * Derived d=new Derived();
* * * * * * * d.Dispatch(); //trys to delegate bases
* * * * }
}
The same thing happens when Base is NOT abstract class and Foo is
declared as virtual....
Is there a rule don't mix delegates and runtime polymorphism???
Great! let me get the debug, i thing my telling compiler to think
Base is Base.....
Oct 21 '08 #4

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

Similar topics

1
1585
by: Kalyan | last post by:
Hello, The following posting was made on this newsgroup some 2 yrs ago. Is this still true with the present release of .net. Because I get the exact same exception thrown "Function pointer not...
4
22855
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
2
1180
by: OpticTygre | last post by:
Ok, so I've been trying and trying to learn about delegates, but I still can't grasp the concept of the advantages. Why use delegate functions instead of calling the function directly? In other...
6
2482
by: Jon Davis | last post by:
I've used delegates fairly heavily for several years in C# for event handling and for starting threads. Does anyone have any real-world scenarios where delegates were both extremely useful and...
0
4755
by: bharathreddy | last post by:
Delegates Here in this article I will explain about delegates in brief. Some important points about delegates. This article is meant to only those who already know delegates, it will be a quick...
7
3406
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
0
834
by: DahrkDaiz | last post by:
Ok so I have a base class and several inherited classes. The base class has 3 Delegate Objects so that the inherited classes can redefine search functions (note, virtual will not work in this case)....
69
5516
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...
3
4831
by: David K in San Jose | last post by:
I'm using managed (CLR) C++ in VS2005 to create a Windows app that contains a form named "MyForm". In the code for that form I'm trying to invoke some static functions by using an array of function...
0
7160
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
7537
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...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5086
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...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1594
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.