473,322 Members | 1,699 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,322 software developers and data experts.

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(MyFoo1) );
dels.Add( "key", new Foo2Delegate(MyFoo2) );
dels.Add( "key", new Foo3Delegate(MyFoo3) );

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 2188

"Stephen Johns" <bl*********@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.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(MyFoo1) );
dels.Add( "key", new Foo2Delegate(MyFoo2) );
dels.Add( "key", new Foo3Delegate(MyFoo3) );

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*********@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.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(MyFoo1) );
dels.Add( "key", new Foo2Delegate(MyFoo2) );
dels.Add( "key", new Foo3Delegate(MyFoo3) );

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 "typesafeness" 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*********@yahoo.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(MyFoo1) );
dels.Add( "key", new Foo2Delegate(MyFoo2) );
dels.Add( "key", new Foo3Delegate(MyFoo3) );

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.Collections;

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.WriteLine ("Hello");
}

static void Bar()
{
Console.WriteLine ("There");
}
}
--
Jon Skeet - <sk***@pobox.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Stephen Johns <bl*********@yahoo.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(MyFoo1) );
dels.Add( "key", new Foo2Delegate(MyFoo2) );
dels.Add( "key", new Foo3Delegate(MyFoo3) );

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.Collections;

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.WriteLine ("Hello");
}

static void Bar()
{
Console.WriteLine ("There");
}
}
--
Jon Skeet - <sk***@pobox.com>
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, "typesafeness" 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(int i);
delegate void Foo2Delegate(char ch);
delegate void Foo3Delegate(string s);

static void Main()
{
Hashtable dels = new Hashtable();
dels.Add( "key1", new Foo1Delegate(MyFoo1) );
dels.Add( "key2", new Foo2Delegate(MyFoo2) );
dels.Add( "key3", new Foo3Delegate(MyFoo3) );

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*********@yahoo.com> wrote in message
news:en****************@TK2MSFTNGP10.phx.gbl...


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

I guess I am just old school, "typesafeness" 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
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
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>...
4
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...
6
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...
2
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"...
7
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...
2
by: qazmlp1209 | last post by:
class base { public: base() { } base(int number) { priNumber = number ;
3
by: Rob | last post by:
I have these classes (elided methods): class Base { public: Base(string name) {...} }; class Derived : public Base {
3
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.