I have been scratching my head over this for days and I still cannot understand how to implement this interface.
Here is my code:
namespace ConsoleApplication32 {
public static class ScanAndSerialize
{
public static void Serialize()
{
List<string> dirs = FileHelper.GetFilesRecursive("s:\\");
List<string> dirFiles = new List<string>();
foreach (string p in dirs)
{
string path = p;
string lastAccessTime = File.GetLastAccessTime(path).ToString();
bool DirFile = File.Exists(path);
DateTime lastWriteTime = File.GetLastWriteTime(p);
//dirFiles.Add(p + " , " + lastAccessTime.ToString() + " , " + DirFile.ToString() + " , " + lastWriteTime.ToString());
dirFiles.Add(p);
dirFiles.Add(lastAccessTime);
dirFiles.Add(DirFile.ToString());
dirFiles.Add(lastWriteTime.ToString());
dirFiles.Add(Environment.NewLine);
}
XmlSerializer SerializeObj = new XmlSerializer(dirFiles.GetType());
string sDay = DateTime.Now.ToString("MMdd");
string fileName = string.Format(@"s:\project\{0}_file.xml", sDay);
TextWriter WriteFileStream = new StreamWriter(fileName);
SerializeObj.Serialize(WriteFileStream, dirFiles);
WriteFileStream.Close();
}
static class FileHelper
{
public static List<string> GetFilesRecursive(string b)
{
// 1.
// Store results in the file results list.
List<string> result = new List<string>();
// 2.
// Store a stack of our directories.
Stack<string> stack = new Stack<string>();
// 3.
// Add initial directory.
stack.Push(b);
// 4.
// Continue while there are directories to process
while (stack.Count > 0)
{
// A.
// Get top directory
string dir = stack.Pop();
try
{
// B
// Add all files at this directory to the result List.
result.AddRange(Directory.GetFiles(dir, "*.*"));
// C
// Add all directories at this directory.
foreach (string dn in Directory.GetDirectories(dir))
{
stack.Push(dn);
}
}
catch
{
// D
// Could not open the directory
}
}
return result;
}
}
public class MyInterface: IValidationRowSet
{
public int RowNumber { get; set; }
public string RowAsString { get; set; }
public IValidationRowSet MatchedRow { get; set; }
public string FriendlyNameLabel { get; set; }
public string KeyFieldLabel { get; set; }
IList<string> lst = new List<string>();
public string SourceWorksheetName { get; set; }
public string SourceRangeName { get; set; }
//public string SourceRangeName { get; set; }
public bool bReported { get; set; }
public int FieldCount { get { return lst.Count; } }
public string FieldData(int id)
{
if (id <= lst.Count)
return lst[id];
else
return null;
}
public string ValidationMessage { get; set; }
}
Here is an explanation of the interface (still scratching my head over this one)
namespace Validation {
/// <summary>
/// Implement this interface if you want the engine to callback when it finds exception
/// messages. You will pass a reference to you class to the validation engine, and
/// it will call "PostValidationMessage" for each exception example, including the message,
/// the entire row set of data (vr), and the id of the field that created the exception.
/// </summary>
public interface IValidationReporter
{
/// <param name="sMsg"></param>
/// <param name="vr"></param>
/// <param name="id"></param>
void PostValidationMessage(string sMsg, IValidationRowSet vr, int id);
}
/// <summary>
/// Implement this interface in order to use the validation engine.
/// The validation engine takes 2 IList<IValidationRowSet> objects and compares them.
/// A class that implements this interface will contain an entire row of data that you'll
/// want to compare.
/// </summary>
public interface IValidationRowSet
{
/// <summary>
/// should return an int of the number of fields in this row
/// </summary>
int FieldCount { get; }
/// <summary>
/// should return an int of the row number that this row is in the set
/// usually set when the data is assembled
/// </summary>
int RowNumber { get; set; }
/// <summary>
/// this is a function that should return the field data for this row at zero-indexed location "id"
/// ex: if the row contains this data: smith|fred|2126782524|[email protected]|
/// a call on this method of FieldData(2) will return the phone number 2126782524
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
string FieldData(int id);
/// <summary>
/// this will be modified by the validation process
/// </summary>
string ValidationMessage { get; set; }
/// <summary>
/// this will be modified by the validation process
/// </summary>
IValidationRowSet MatchedRow { get; set; }
/// <summary>
/// returns a string that uniquely identifies this row
/// ex: if the row contains this data: smith|fred|2126782524|[email protected]|
/// so for this example, the unique identifier could be the email address [email protected]
/// </summary>
string KeyFieldLabel { get; set; }
/// <summary>
/// returns a string with the "friendly" name of this row
/// ex: if the row contains this data: smith|fred|2126782524|[email protected]|
/// so for this example, FriendlyNameLabel could be the name, such as "Fred Smith"
/// </summary>
string FriendlyNameLabel { get; set; }
/// <summary>
/// returns all fields in the row as pipe delimited
/// ex: 1,234.23|Fred Smith|[email protected]|
/// </summary>
string RowAsString { get; set; }
/// <summary>
/// if this is an excel file comparison, this should return the name
/// of the worksheet from whence this data came
/// </summary>
string SourceWorksheetName { get; set; }
/// <summary>
/// if this is an excel file comparison, this should return the name
/// of the worksheet range from whence this data came
/// </summary>
string SourceRangeName { get; set; }
/// <summary>
/// this will be modified by the validation process
/// </summary>
bool bReported { get; set; }
}
}
I have read NUMEROUS articles/books/forum postings about Interfaces. This concept feels like a black hole to me...and i'm on a project where i have to implement this. Anybody have ANY idea how the heck you implement this? By the way--i'm a COMPLETE newbie programmer...less than 2 months experience...therefore please do not chastise me for my green-ness please.
Thanks in advance.