I'm trying to learn C# and I'm a bit stuck with a problem. Probably obvious for most of experienced programmers, but not for me :(
I'm trying to write a class that is supposed to have 3 constructor overloads:
- I only specify the folder.
- I specify the folder and the file type (file extension).
- I specify the folder, file type and yes/no for sub folder search.
Depending on the user specification the class returns a List.
So I wrote the following code:
public class SearchForFiles
{
private readonly List<string> _filePath = new List<string>();
private readonly List<string> _fileType = new List<string>();
private readonly bool _searchSubFolder = false;
private SearchOption _searchOption = SearchOption.TopDirectoryOnly;
private readonly IEnumerable<string> _searchForFiles = null;
/// <summary>
/// Searching all the files in the specified folder.
/// </summary>
/// <param name="searchFolder">Specify folder for file search</param>
public SearchForFiles(string searchFolder)
{
_filePath.Add(searchFolder);
_fileType.Add(".*");
FindFiles(_filePath, _fileType, false);
}
/// <summary>
/// Searching all the files with the specified file extension in the specified folder.
/// </summary>
/// <param name="searchFolder">Specify folder for file search</param>
/// <param name="fileType">Specify extension of the file search</param>
public SearchForFiles(string searchFolder, string fileType)
: this(searchFolder)
{
_fileType.Add(fileType);
FindFiles(_filePath, _fileType, false);
}
public SearchForFiles(List<string> searchFolder, List<string> fileType, bool searchSubFolder)
: this(searchFolder, fileType)
{
_searchSubFolder = searchSubFolder;
FindFiles(_filePath, _fileType, _searchSubFolder);
}
It's the third overload that's the problem, I don't understand why the second overload is all right but not the third.
If anybody could give me some help that would be great.
Here is the updated Code that works:
/// <summary>
/// Returning a list of files depending on the user choices.
/// </summary>
public class SearchForFiles
{
private readonly List<string> _filePath = new List<string>();
private readonly List<string> _fileType = new List<string>();
private readonly bool _searchSubFolder = false;
private SearchOption _searchOption = SearchOption.TopDirectoryOnly;
private IEnumerable<string> _searchForFiles = null;
/// <summary>
/// Searching all the files with the specified file extension(s) in the specified folder(s) and sub folder(s) if wanted.
/// </summary>
/// <param name="searchFolder">Specify folder(s) for file(s) search</param>
/// <param name="fileType">Specify file extension(s) for the search</param>
/// <param name="searchSubFolder">Specify if you want to search in sub folder(s)</param>
public SearchForFiles(List<string> searchFolder, List<string> fileType, bool searchSubFolder)
{
_filePath = searchFolder;
_fileType = fileType;
_searchSubFolder = searchSubFolder;
FindFiles();
}
/// <summary>
/// Searching all the files with the specified file extension in the specified folder.
/// </summary>
/// <param name="searchFolder">Specify folder for file search</param>
/// <param name="fileType">Specify file extension for the search</param>
public SearchForFiles(string searchFolder, string fileType)
: this(new List<string>() {searchFolder}, new List<string>() {fileType}, false)
{
}
/// <summary>
/// Searching all the files in the specified folder.
/// </summary>
/// <param name="searchFolder">Specify folder for file search</param>
public SearchForFiles(string searchFolder)
: this(new List<string>() {searchFolder}, new List<string>(), false)
{
}
private List<string> FindFiles()
{
var findFiles = new List<string>();
if (_searchSubFolder == true)
_searchOption = SearchOption.AllDirectories;
foreach (var filePath in _filePath)
{
if (_fileType.Count == 0)
{
_searchForFiles = Directory.EnumerateFiles(filePath, "*.*", _searchOption);
}
else
{
_searchForFiles = Directory.EnumerateFiles(filePath, "*.*", _searchOption).Where(e => _fileType.Contains(new FileInfo(e).Extension, StringComparer.OrdinalIgnoreCase));
}
foreach (var file in _searchForFiles)
{
findFiles.Add(file);
}
}
return findFiles;
}
}
Thanks a lot for all the hints and answers :)
List<string>but the second overload constructor need to passstring. the type is different.*.*as a file type) and then add the file type the caller specified. Instead, have one one arg version invoke the two are version with the default ‘*.*`. You might consider just an optional arg instead. You didn't mention the the error you are getting, but I'm guessing it has to do with passing a list of string where a string is expecFindFilesin the constructors, does it change the object's state, or does it return the files it finds. If the latter, it belongs in an instance method. Finally, you don't overload constructors, a class can just have more than one declared.