Solved

C#, Generic, Reuse of file enumeration

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

937 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now