473,785 Members | 2,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

No delegate "base class" ?!?!?!


I want to have a Hashtable whose keys are strings and whose values are
delegates.

Well, ok, I have that:

dels = new Hastable();
dels.Add( "key", new Foo1Delegate(My Foo1) );
dels.Add( "key", new Foo2Delegate(My Foo2) );
dels.Add( "key", new Foo3Delegate(My Foo3) );

This works.

BUT! I can't get the darned things back out!

I want to do:

delegate d = (delegate)dels["key"];
d( arg );

The first line results in a syntax error.
MyFoo1 f = (MyFoo1)dels["key"];
f( arg );

works, but I have to know the delegate type to get it out - negating
what I am trying to accomplish.

All the methods I will call have the same arguments.

Basically looking for C/Function-Pointer kind of thing.

Anyone know how to do this in C#?

Any help greatly appreciated.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
6 2203

"Stephen Johns" <bl*********@ya hoo.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..

I want to have a Hashtable whose keys are strings and whose values are
delegates.

Well, ok, I have that:

dels = new Hastable();
dels.Add( "key", new Foo1Delegate(My Foo1) );
dels.Add( "key", new Foo2Delegate(My Foo2) );
dels.Add( "key", new Foo3Delegate(My Foo3) );

This works.

BUT! I can't get the darned things back out!

I want to do:

delegate d = (delegate)dels["key"];
d( arg );
Use the type Delegate. You will probably have to use DynamicInvoke to
perform the work, but
it should work(untested).

The first line results in a syntax error.
MyFoo1 f = (MyFoo1)dels["key"];
f( arg );

works, but I have to know the delegate type to get it out - negating
what I am trying to accomplish.

All the methods I will call have the same arguments.

Basically looking for C/Function-Pointer kind of thing.

Anyone know how to do this in C#?

Any help greatly appreciated.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #2

"Stephen Johns" <bl*********@ya hoo.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..

I want to have a Hashtable whose keys are strings and whose values are
delegates.

Well, ok, I have that:

dels = new Hastable();
dels.Add( "key", new Foo1Delegate(My Foo1) );
dels.Add( "key", new Foo2Delegate(My Foo2) );
dels.Add( "key", new Foo3Delegate(My Foo3) );

This works.

BUT! I can't get the darned things back out!

I want to do:

delegate d = (delegate)dels["key"];
d( arg );

The first line results in a syntax error.
MyFoo1 f = (MyFoo1)dels["key"];
f( arg );

works, but I have to know the delegate type to get it out - negating
what I am trying to accomplish.


Thus the "typesafene ss" of delegates.

Why don't you just define an interface, and store interface references
insead of delegates.

David

Nov 15 '05 #3
Stephen Johns <bl*********@ya hoo.com> wrote:

I want to have a Hashtable whose keys are strings and whose values are
delegates.

Well, ok, I have that:

dels = new Hastable();
dels.Add( "key", new Foo1Delegate(My Foo1) );
dels.Add( "key", new Foo2Delegate(My Foo2) );
dels.Add( "key", new Foo3Delegate(My Foo3) );

This works.

BUT! I can't get the darned things back out!


<snip>

I think you're looking for the Delegate class. Try this, for instance:

using System;
using System.Collecti ons;

class Test
{
delegate void X();
delegate void Y();

static void Main()
{
ArrayList al = new ArrayList();
al.Add(new X(Foo));
al.Add(new Y(Bar));
foreach (Delegate d in al)
{
d.DynamicInvoke (null);
}
}

static void Foo()
{
Console.WriteLi ne ("Hello");
}

static void Bar()
{
Console.WriteLi ne ("There");
}
}
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Stephen,

As Jon and Daniel note, you can use Delegate and then call DynamicInvoke()
on the delegate. You should note that this is considerably slower than
calling directly through a delegate (at least 10x slower, and perhaps even
100x slower) because of all the work required to do a late-bound call.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Stephen Johns <bl*********@ya hoo.com> wrote:

I want to have a Hashtable whose keys are strings and whose values are
delegates.

Well, ok, I have that:

dels = new Hastable();
dels.Add( "key", new Foo1Delegate(My Foo1) );
dels.Add( "key", new Foo2Delegate(My Foo2) );
dels.Add( "key", new Foo3Delegate(My Foo3) );

This works.

BUT! I can't get the darned things back out!


<snip>

I think you're looking for the Delegate class. Try this, for instance:

using System;
using System.Collecti ons;

class Test
{
delegate void X();
delegate void Y();

static void Main()
{
ArrayList al = new ArrayList();
al.Add(new X(Foo));
al.Add(new Y(Bar));
foreach (Delegate d in al)
{
d.DynamicInvoke (null);
}
}

static void Foo()
{
Console.WriteLi ne ("Hello");
}

static void Bar()
{
Console.WriteLi ne ("There");
}
}
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #5


Thanks for the info guys! That is what I am looking for.

I guess I am just old school, "typesafene ss" hmmmm... sounds like
training wheels... Give me a (void *) anyday, I'll remember what I put
in there I'd never accidentally try and "call" a piece of data! hahahaha
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #6
Stephen,
Have you tried something like (tested in VS.NET 2003):

delegate void Foo1Delegate(in t i);
delegate void Foo2Delegate(ch ar ch);
delegate void Foo3Delegate(st ring s);

static void Main()
{
Hashtable dels = new Hashtable();
dels.Add( "key1", new Foo1Delegate(My Foo1) );
dels.Add( "key2", new Foo2Delegate(My Foo2) );
dels.Add( "key3", new Foo3Delegate(My Foo3) );

object o = dels["key2"];
if (o is Foo1Delegate)
{
Foo1Delegate d = (Foo1Delegate)o ;
d(1);
}
else if (o is Foo2Delegate)
{
Foo2Delegate d = (Foo2Delegate)o ;
d(' ');
}
else if (o is Foo3Delegate)
{
Foo3Delegate d = (Foo3Delegate)o ;
d("");
}
}

static void MyFoo1(int i)
{
}

static void MyFoo2(char ch)
{
}

static void MyFoo3(string s)
{
}

Hope this helps
Jay

"Stephen Johns" <bl*********@ya hoo.com> wrote in message
news:en******** ********@TK2MSF TNGP10.phx.gbl. ..


Thanks for the info guys! That is what I am looking for.

I guess I am just old school, "typesafene ss" hmmmm... sounds like
training wheels... Give me a (void *) anyday, I'll remember what I put
in there I'd never accidentally try and "call" a piece of data! hahahaha
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #7

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

Similar topics

14
1911
by: mshetty | last post by:
Hi, I get an error "Warning: b::a_method hides the virtual function a::a_method()." on compiling the following code.. #include <iostream.h> class a {
11
2104
by: Joseph Turian | last post by:
Fellow hackers, I have a class BuildNode that inherits from class Node. Similarly, I have a class BuildTree that inherits from class Tree. Tree includes a member variable: vector<Node> nodes; // For clarity, let this be "orig_nodes" BuildTree includes a member variable:
4
1762
by: Gecko | last post by:
I noticed that every time I override a class member, the intellisense behavior is to automatically add a call to the base class of the overridden member such as: public override void SomeMethod() { /// Some quality code goes here. base.SomeMethod(); }
6
11311
by: SA | last post by:
Hi all: I have an object of a base class that needs to be cast to an object of a specialized class. What is the best way to do this? (I thought about creating a constructor in the specialized class that takes an argument of the type of the base class and then copy property values over, but that seems like a hassle) --
2
1718
by: Wade | last post by:
Hi all, We have created some "Base" class pages for our WebForms and UserControls. For instance, when we create a WebForm called "WebForm1.aspx", instead of inheriting from "System.Web.UI.Page" we implement from our "Base" class page which itself inherits from "System.Web.UI.Page" -- I know, pretty standard. We do the same with our UserControls, instead they inherit from "System.Web.UI.UserControl". Now, there are some methods that we...
7
2249
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to me. So, I'm here for an answer (more of a confirmation), hopefully. First let me say that I know people keep saying; it doesn't work because the member "is a private". I believe there's more to it than just simply that... Theory: You inherit,...
2
2199
by: qazmlp1209 | last post by:
class base { public: base() { } base(int number) { priNumber = number ;
3
1589
by: Rob | last post by:
I have these classes (elided methods): class Base { public: Base(string name) {...} }; class Derived : public Base {
3
1669
by: Ravi | last post by:
Is this the correct way to think of "base class"? The "base class" is a class from which other classes are derived. The "base class" will never be derived from another class.
0
9643
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
10319
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
10147
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...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
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
6737
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
5380
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...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
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

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.