In this scnario, the program gets the xmlfiles in a directory. Each xmlfile is being evaluated in second method if it is already added in listToWatch List. However, firstMethod is also looped for evaluation of each directory (which is not written below).
The program detects all files in the xml file which are already added. But if the program goes to another directory (because firstMethod is looped in another class), the listToWatch = new List() is passed by, erasing the previous listToWatch and creating a new object.
I want to use the same object without being overwritten with a new list. I can't put listToWatch = new List in secondMethod because there's a for loop and it will just overwrite the listToWatch with a new object. I can't put it either inside firstMethod because it needs to be set in the secondMethod. I can't also put it inside class Sample. Where should I put listToWatch = new List()?
class Sample
{
public static List<string> listToWatch
{
get;
set;
}
public static void firstMethod()
{
string getFiles = Directory.GetFiles(directory, "*.xml");
foreach (string xmlFile in getFiles)
{
secondMethod(xmlFile);
}
}
public static void secondMethod(xmlFile)
{
listToWatch = new List<string>();
foreach (string file in xmlFile)
{
if (listToWatch.Contains(file))
{
sw.WriteLine(file + " is already added!");
}
else
{
listToWatch.add();
}
}
}
I can't put it either inside firstMethod because it needs to be set in the secondMethod- why ??, that is probably the best place to initialize itListtoHashSetto more easily prevent duplicates, and improve performance. Adding without duplicating simply becomeslistToWatch.Add(file). msdn.microsoft.com/en-us/library/bb359438.aspx