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();
}
}
}
what about using it with getter and setter?
Must say, probably are better options to let the method return this object instead of store it, but if for some reason you can’t change that, I think this will work
edit: Thanks to @gorpik, this is called “property” and not “using getter and setter“.