Solved

C#, Generic, Reuse of file enumeration

Posted on 2009-05-13
2
251 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
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 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Service Controller 1 23
Programmatically storing deadlock graphs with SQL Server 6 41
Printing 1 51
application restarter  - for an exe that hangs 9 27
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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