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

Passing function as an argument and executing

I know you can do this in C++ as I remember doing it in my college days
(just a few years ago), but I'm having trouble finding reference on how
to do it with a managed language.

What I have is a wrapper function that performs a somewhat complex
enumeration of different objects that can be used to accomplish many
different tasks on each object.

I have each task seperated into it's own function because embedding all
the functions into an endless if/then/else inside the wrapper function
would be rather hard to read, and poor coding to boot.

What I'd like to do is somehow pass a reference to a single one of
these functions to the wrapper function when it is invoked, so that
when it has waded through all the enumeration logic it can then invoke
the "worker" function with proper arguments.
It this possible, and if so, how can I implement it?

Thanks,
Ray

Nov 16 '05 #1
3 1466
What you want to do is create a delegate for this function / method.

The C# equivalent of C++ function pointers is delegates. Yes, delegates
are often used in conjunction with events, and are also used for
multicast, but you can also use simple delegates as function pointers.

Nov 16 '05 #2
Excellent! Thanks for the tip!

Nov 16 '05 #3
Ray... The general solution when faced with a switch is to abstract the
action
into an interface. Then write an adapter or helper class that
implements the abstract interface and contains the state for each
object. Pass
the adapter to the caller and the caller invokes the abstract function
call. The
adapter knows its own state and invokes the appropriate action. No
switch
needed. By converting from a switch to a stateful object, you support
extensibility. You can write a new class adapter for a new class and
pass the
new adapter to the client. Now if this makes no sense I am still writing
a
chapter on dynamic class factories, but some code is up at:
http://www.geocities.com/jeff_louie/OOP/oop18.htm

Regards,
Jeff
What I'd like to do is somehow pass a reference to a single one of

these functions to the wrapper function when it is invoked, so that
when it has waded through all the enumeration logic it can then invoke
the "worker" function with proper arguments.<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4

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

Similar topics

8
by: Giulio | last post by:
I'm doing a class who takes in constructor a vector<int> keeps and makes some statistic on the vector whitout modifiing it. I would like not to make a copy of the vector for the class. to let the...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
7
by: Richard Grant | last post by:
Hi. In c/C++ i can pass the address of a subroutine to another subroutine as an actual parameter How do I do that in VB .NET What should be the syntax for a parameter to receive the address of a...
1
by: User1014 | last post by:
Since you can pass a function to a ... erm...... function.... how to you use the result of a function as the argument for another function instead of passing the actual function to it. i.e. ...
12
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned...
2
by: PatrickMinnesota | last post by:
I've been reading the docs and looking for an answer and seem stuck. I'm either not looking in the right places or not understanding what I'm reading. I have a bunch of functions. I want to put...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.