473,322 Members | 1,538 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.

Passing a method as a parameter

Hello,

I'm creating a toolbox of useful functions, and I've added a function
that recursively drills down through a file system searching for files.
I've created a number of variations of this function, and I imagine
I'll continue to have use for it.

What I would like to do is create a version that will take a method as
a parameter and invoke it as it traverses my file system path. Here is
some pseudo-code that will hopefully illustrate what I'm trying to do:

public class Toolbox{
public void (string startDirectory, Method myMethod) fileDriller{
foreach (string f in Directory.GetFiles(startDirectory){
myMethod(f);
}
foreach (string d in Directory.GetDirectories(startDirectory){
fileDriller(d, myMethod);
}
}
}

So let's say I wanted to determine the length of each filename in a
directory of interest and every subdirectory:

public void lengthOfFileName(string fileName){
Console.write(fileName.Length.toString());
}

static void Main(){
Toolbox tb = new Toolbox();
tb.fileDriller("C:\\somedir", lengthOfFileName);
}

I tried passing a MethodInfo object, but I'm running into issues
because, I believe, it's an abstract class. If anyone has any idea how
I would accomplish this, please let me know!

Regards,

Chris

Oct 12 '06 #1
2 1482
If the signature of your method is known, use a delegate:

public void delegate FileMethod(string fileName);

public void FileDriller(string startDirectory, FileMethod myMethod)
{
foreach (string f in Directory.GetFiles(startDirectory))
myMethod(f);

foreach (string d in Directory.GetDirectories(startDirectory))
FileDriller(d, myMethod);
}

Dustin Campbell
Developer Express Inc.
Oct 12 '06 #2
You could do this using delegates, or using the "strategy" design
pattern.

Delegates
http://msdn2.microsoft.com/en-us/lib...71(VS.80).aspx

Strategy pattern
http://www.dofactory.com/Patterns/PatternStrategy.aspx

Good luck!

hardi...@hotmail.com wrote:
Hello,

I'm creating a toolbox of useful functions, and I've added a function
that recursively drills down through a file system searching for files.
I've created a number of variations of this function, and I imagine
I'll continue to have use for it.

What I would like to do is create a version that will take a method as
a parameter and invoke it as it traverses my file system path. Here is
some pseudo-code that will hopefully illustrate what I'm trying to do:

public class Toolbox{
public void (string startDirectory, Method myMethod) fileDriller{
foreach (string f in Directory.GetFiles(startDirectory){
myMethod(f);
}
foreach (string d in Directory.GetDirectories(startDirectory){
fileDriller(d, myMethod);
}
}
}

So let's say I wanted to determine the length of each filename in a
directory of interest and every subdirectory:

public void lengthOfFileName(string fileName){
Console.write(fileName.Length.toString());
}

static void Main(){
Toolbox tb = new Toolbox();
tb.fileDriller("C:\\somedir", lengthOfFileName);
}

I tried passing a MethodInfo object, but I'm running into issues
because, I believe, it's an abstract class. If anyone has any idea how
I would accomplish this, please let me know!

Regards,

Chris
Oct 12 '06 #3

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

Similar topics

5
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a...
7
by: Robert Lario | last post by:
For examples sake I have made up a very simple example. I have an object called foo1 which is of type foo. I want to be able to call a funtion called myfunc as follows: myfunc(ref foo1) ...
6
by: Catherine Jones | last post by:
Hi all, we need urgent help in a matter. We are trying to pass a COM object from the client to server and are facing some problems in the same. We've our client in C# as well as the Server...
7
by: Ken Allen | last post by:
I have a .net client/server application using remoting, and I cannot get the custom exception class to pass from the server to the client. The custom exception is derived from ApplicationException...
8
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects,...
3
by: Mark | last post by:
Hi From what I understand, you can pass arrays from classic ASP to .NET using interop, but you have to change the type of the.NET parameter to object. This seems to be because classic ASP passes...
5
by: blue | last post by:
We often get connection pooling errors saying that there are no available connections in the pool. I think the problem is that we are passing around open readers all over the place. I am...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
10
by: Janus | last post by:
Hi, Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this: var...
4
by: Deckarep | last post by:
Hello fellow C# programmers, This question is more about general practice and convention so here goes: I got into a discussion with a co-worker who insisted that as a general practice all...
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
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
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...

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.