Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C#, Generic, Reuse of file enumeration

Posted on 2009-05-13
2
Medium Priority
?
265 Views
Last Modified: 2013-12-17
I'm trying to use my FileWorks class below to tell me if a directory has a certain file extension.
I'm trying to be as generic as possible and reuse the function...
GetFilesDoSomething

I'm not sure how to approach this one. This is the code I have below.

Does not look like it would work if I were to call it...

FileWorks filework = new FileWorks();
filework.GetFilesDoSomething(directory, CheckDirectoryForFilesWithExtension);

How can I pass the bool result back accross soo many functions?


public class FileWorks
    {
 
        bool CheckDirectoryForFilesWithExtension(FileInfo fi)
        {
            if (fi.Extension == "tif")
            {
                return true;
            }
        }
 
 
        public void GetFilesDoSomething(string DirName, Action<FileInfo> method)
        {
            DirectoryInfo d = new DirectoryInfo(DirName);
            FileInfo[] f = d.GetFiles();
 
            foreach (FileInfo fi in f)
            {
                method.Invoke(fi);
            }
        }
     }

Open in new window

0
Comment
Question by:silentthread2k
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Expert Comment

by:ToddBeaulieu
ID: 24377680
This looks like delegates 101, not generics.

Check this out:

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_24247143.html


0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 2000 total points
ID: 24378736
This codes isn't really "proper", but it does demonstrate using a delegate to make your GetFilesDoSomething() re-usable.
class Program
{
	static void Main(string[] args)
	{
 
 
		FileWorks.GetFilesDoSomething("C:\\", new FileWorks.SomethingDelegate(FileWorks.CheckDirectoryForFilesWithExtension));
 
		Console.ReadKey();
	
	}
 
}
 
public static class FileWorks
{
	public delegate void SomethingDelegate(object inParam, out object returnVal);
 
	public static void CheckDirectoryForFilesWithExtension(object fi, out object returnValue)
	{
		if (((FileInfo)fi).Extension.ToLower() == ".tif")
			returnValue = true;
		else
			returnValue = false;
 
		return;
	}
 
	public static void GetFilesDoSomething(string dirName, SomethingDelegate method)
	{
		DirectoryInfo d = new DirectoryInfo(dirName);
		FileInfo[] f = d.GetFiles();
 
		foreach (FileInfo fi in f)
		{
			object returnValue;
			method(fi, out returnValue);
 
			Console.WriteLine("{0} is a TIF: {1}", fi.Name, (bool)returnValue);
		}
	}
}

Open in new window

0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question