By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,316 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

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

P: n/a

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
Share this Question
Share on Google+
6 Replies


P: n/a

"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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.