Hi
I have the following object oriented code that I'm trying to figure out why my member variable is empty.
I'm having an issue with a problem I'm working on and I'm seeking some help on what to try. I've got the following code but not sure why my member variable mPath does not seem to get an initial value in my Constructor. I'm not looking for someone to give me the code, but trying to figure out why mPath is empty or what is going on. Any help would be greatly appreciated.
I have the following object oriented code that I'm trying to figure out why my member variable is empty.
I'm having an issue with a problem I'm working on and I'm seeking some help on what to try. I've got the following code but not sure why my member variable mPath does not seem to get an initial value in my Constructor. I'm not looking for someone to give me the code, but trying to figure out why mPath is empty or what is going on. Any help would be greatly appreciated.
C#:
using System.IO;
using System;
public class Client
{
public virtual bool ProcessFile(string name)
{
return (true);
}
public virtual bool ProcessDirectory(string name)
{
return (true);
}
}
public class PrintingClient : Client
{
public override bool ProcessFile(string name)
{
Console.WriteLine("file: {0}", name);
return (true);
}
public override bool ProcessDirectory(string name)
{
Console.WriteLine("directory: {0}", name);
return (true);
}
}
public class Scanner
{
public string TheDirectory { get; set; }
public Client TheClient { get; set; }
public bool Recurse { get; set; }
public Scanner(string theDirectory, Client theClient, bool recurse)
{
TheDirectory = theDirectory;
TheClient = theClient;
Recurse = recurse;
}
public Scanner(string theDirectory) :
this(theDirectory, new Client(), true)
{
}
public Scanner(string theDirectory, bool recurse) :
this(theDirectory, new Client(), recurse)
{
}
public Scanner() : this(".", new Client(), true)
{
}
public void Execute()
{
ScanDirectory(TheDirectory);
}
private bool ScanDirectory(string theDirectory)
{
string[] files = Directory.GetFiles(theDirectory);
foreach (string file in files)
if (!TheClient.ProcessFile(file))
return (false);
string[] dirs = Directory.GetDirectories(theDirectory);
foreach (string dir in dirs)
if (!TheClient.ProcessDirectory(dir))
return (false);
if (Recurse)
{
foreach (string dir in dirs)
if (!ScanDirectory(dir))
return (false);
}
return (true);
}
public class LargestFile : Client
{
private long mLargestSize = 0;
private string mLargestName = "";
public override bool ProcessFile(string name)
{
FileInfo fi = new FileInfo(name);
if (fi.Length > mLargestSize)
{
mLargestName = name;
mLargestSize = fi.Length;
}
return (true);
}
public void ShowLargest()
{
Console.WriteLine("largest-file : {0}", mLargestName);
Console.WriteLine("largest-file-size: {0}", mLargestSize);
}
}
public class SearchForFile : Client
{
public SearchForFile(string fileName)
{
mSearchFor = fileName;
mPath = "";
}
public override bool ProcessFile(string name)
{
FileInfo fi1 = new FileInfo(name);
mSearchFor = name;
// mPath = "";
if (mSearchFor == fi1.Name)
{
mPath = fi1.FullName;
return (false);
}
else
{
mSearchFor = "Not Found";
mPath = "Not Found";
return (true);
}
}
public void ShowSearchResults()
{
Console.WriteLine("Search-for: {0}", mSearchFor);
Console.WriteLine("File-Location: {0}", mPath);
}
public String mSearchFor = "";
public String mPath = "";
}
public static void Main()
{
LargestFile lf = new LargestFile();
SearchForFile sff = new SearchForFile("one");
var theScanner = new Scanner("c:\\temp", true);
theScanner.TheClient = lf;
theScanner.Execute();
lf.ShowLargest();
theScanner.TheClient = sff;
sff.ShowSearchResults();
Console.Read();
}
}
Last edited by a moderator: