2

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();
            }
        }
    }
4
  • 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 it Commented Dec 13, 2012 at 12:44
  • Or even better, let the methods return a List instead of trying to store it in the class. Commented Dec 13, 2012 at 12:45
  • 1
    If the order of files is unimportant, I'd suggest switching List to HashSet to more easily prevent duplicates, and improve performance. Adding without duplicating simply becomes listToWatch.Add(file). msdn.microsoft.com/en-us/library/bb359438.aspx Commented Dec 13, 2012 at 12:48
  • As far as I can tell the xmlFile parameter passed into secondMethod is a string, so your foreach in that method is iterating over the characters in that string. This doesn't seem remotely correct to me. Commented Dec 13, 2012 at 13:09

5 Answers 5

8

what about using it with getter and setter?

private static List<string> _ListToWatch;
public static List<string> ListToWatch
{
    get
        {
         if(_ListToWatch == null) 
              _ListToWatch = new List();
          return _ListToWatch;
         }
    set 
     {
      _ListToWatch = value;
     }

}

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".

Sign up to request clarification or add additional context in comments.

4 Comments

The name for that is property.
Setter presence is dubious - the list is meant to be created only inside the class.
@Gonzalo.- You can put it directly in your answer, no need for attributions.
@MaxYakimets I agree, probably the setter should be private.
1

You can either initialize listToWatch in the static constructor or use the method Gonzalo is suggesting.

class Sample
{
    static Sample()
    {
        listToWatch = new List<string>();
    }

    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)
    {
        foreach (string file in xmlFile)
        {
            if (listToWatch.Contains(file))
            {
                sw.WriteLine(file + " is already added!");
            }
            else
            {
                listToWatch.add();
            }
        }
    }
}

Comments

1

If you are not paranoid about redundant memory allocations (in case of empty dir, empty xml - the list won't be needed), just initialize ListToWatch in the static declaration itself:

class Sample
{
    private static List<string> listToWatch = new List<string>();
    public static List<string> ListToWatch
    {
        get { return listToWatch; };
    }
...
}

Note an absent setter: only this class is allowed to change listToWatch reference.

Comments

0

You just have to move the new List<string>() statement into firstMethod, so that it creates a list of strings once for all the files it's going to add.

3 Comments

This will not work because OP says that firstMethod() is also called in a loop for all applicable directories.
Oh, yes, you're absolutely correct. I hadn't noticed that comment.
Well the question is not very easy to understand :)
0

You just need to create this List once, so make it happen on the firstMethod. Try to isolate the code responsabilities.

I think @Gonzalo.- workaround interesting for your case.

Cheers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.