473,671 Members | 2,484 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 5109
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 StackOverflowEx ception 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.dew rote:
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 StackOverflowEx ception 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
1595
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 created by a delegate". Or is there a work around provided for this. In my case I don't think the methods pointed to by function pointers follow __cdecl calling
4
22876
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 when to use one over another? If anyone could provide any additional info, your comments, best practices, any good articles, specific examples, etc. Thank you
2
1184
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 words, if I had something like: --------------------------------------------- Public Class ProductList Public Function GetTotal() as Decimal
6
2494
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 extremely appropriate (as opposed to other programmatic means or mechanisms) that were NOT related to events and ThreadStarts? Thanks, Jon
0
4766
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 review not a detailed one. Delegates quite simply are special type of object, a delegate just contains the details of a method. One good way to understanding delegates is by thinking of delegates as something that gives a name to a method...
7
3416
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 uses the "WithEvents" and events. There is another example on page 124 that shows how to use delegates to sort an array. I don't understand the difference between events and delegates. Are they redundant features? How do I decide which to use? ...
0
846
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). After creating an object and reassigning the delegate to a new method, the method is called fine, however, inside the new method, I cannot access the member functions that are inherited from the base class. If I leave the delegates alone and use...
69
5565
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 signature for the method (i.e., as below, int Square (int)). Here is an example to show that feature. Note class "UnAwareClass" has its methods Square and Cuber called by a class DelegateClass. This is because these methods in UnAwareClass have the...
3
4839
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 pointers (delegates). I assume I need to use the array< T keyword to allocate an array of delegates, and then initialize the array by setting each array element to the pointers (handles) of the functions I'll be invoking. I've been trying to...
0
8483
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8824
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8603
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7444
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5703
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2818
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 we have to send another system
2
2060
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.