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

How to call delegated methods individually without dynamic binding?

I want to be able to hand off the inner functionality of a loop to a
delegate. However, I also need to be able to call one of a number of
delegates based on the context of the loop ( in this case what level Im
in in a tree ). Using delegates seems like an easy way to implement all
of the management, and is well understood.
Where Im having trouble is invoking a _single_ delegate out of the
invocation list.

Some simple test code:

class DelegateHolder
{
public TestDelegate imATest;

public void RunDelegate()
{
// this works
foreach (TestDelegate wc in imATest.GetInvocationList())
{
wc(1);
}

// this doesnt - runtime exception casting to TestDelegate[]
TestDelegate[] ds;
ds = (TestDelegate[])imATest.GetInvocationList();
ds[0](1);
ds[1](1);
ds[2](1);
}
}

Mar 23 '06 #1
3 1288
Maybe something like:

Delegate[] da = rdel.GetInvocationList();

((RunDel)da[0])();

((RunDel)da[1])();
--
William Stacey [MVP]

<mm****@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
|I want to be able to hand off the inner functionality of a loop to a
| delegate. However, I also need to be able to call one of a number of
| delegates based on the context of the loop ( in this case what level Im
| in in a tree ). Using delegates seems like an easy way to implement all
| of the management, and is well understood.
| Where Im having trouble is invoking a _single_ delegate out of the
| invocation list.
|
| Some simple test code:
|
| class DelegateHolder
| {
| public TestDelegate imATest;
|
| public void RunDelegate()
| {
| // this works
| foreach (TestDelegate wc in imATest.GetInvocationList())
| {
| wc(1);
| }
|
| // this doesnt - runtime exception casting to TestDelegate[]
| TestDelegate[] ds;
| ds = (TestDelegate[])imATest.GetInvocationList();
| ds[0](1);
| ds[1](1);
| ds[2](1);
| }
| }
|
Mar 23 '06 #2


mm****@gmail.com wrote:
I want to be able to hand off the inner functionality of a loop to a
delegate. However, I also need to be able to call one of a number of
delegates based on the context of the loop ( in this case what level Im
in in a tree ). Using delegates seems like an easy way to implement all
of the management, and is well understood.
Where Im having trouble is invoking a _single_ delegate out of the
invocation list.


Why are you using the delegates invocationlist to represent a list of
delegates?

wouldn't IList<MyDelegate> be much better?

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Mar 23 '06 #3
> Why are you using the delegates invocationlist to represent a list of
delegates?

wouldn't IList<MyDelegate> be much better?

I chose to use a delegate because it is a well understood idiom for
someone who wants to pass methods into my class.

Im certainly not adverse to using a collection, but I was trying to do
the c# way.

Mar 23 '06 #4

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

Similar topics

0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
2
by: Vince | last post by:
I'm writing a framework component, and I would like to be able to determine if a particular event has been delegated or not. By event delegation, I mean: control.event += handler (method); I...
2
by: festiv | last post by:
Hi there, I want to learn how the compiler is implementing the dynamic binding. where can i read about this subject (the hole process). thanks.
8
by: Lucas Lemmens | last post by:
Dear pythonians, I've been reading/thinking about the famous function call speedup trick where you use a function in the local context to represent a "remoter" function to speed up the 'function...
26
by: Paul | last post by:
public class A { public A () { // here I would like to call the second version of _ctor, how to accomplish this ? } public A (int a, int b, int c) {
2
by: RobKinney1 | last post by:
Please, can someone please tell us how to call a webbrowser control (navigate2 or navigate) from a worker thread safely? I have stepped through tutorials on the web about delegated calls and...
5
by: darthghandi | last post by:
I've created a class to listen to all interfaces and do a BeginAccept(). Once it gets a connection, it passes the connected socket off and stores it in a List. Next, it continues to listen for...
9
by: Allen | last post by:
The arguments of function is variable. Given function address, argument type and data, how to dynamically call the function? The following is pseudo code. int count = 0; int offset = 0; char...
2
by: 09876 | last post by:
Hi: all I understand the difference between dynamic binding and static binding. But I just wonder what is the point to make the distinction between the dynamic binding and static binding. For...
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.