2

在这个场景中,程序获取目录中的 xmlfiles。如果每个 xmlfile 已添加到 listToWatch 列表中,则将在第二种方法中对其进行评估。然而, firstMethod 也被循环用于评估每个目录(下面没有写)。

该程序会检测 xml 文件中已添加的所有文件。但是如果程序转到另一个目录(因为 firstMethod 在另一个类中循环),则 listToWatch = new List() 会被传递,擦除之前的 listToWatch 并创建一个新对象。

我想使用相同的对象而不被新列表覆盖。我不能将 listToWatch = new List 放在 secondMethod 中,因为有一个 for 循环,它只会用新对象覆盖 listToWatch。我也不能把它放在 firstMethod 中,因为它需要在 secondMethod 中设置。我也不能把它放在类 Sample 中。我应该把 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

5 回答 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".

于 2012-12-13T12:45:44.710 回答
1

如果您对冗余内存分配没有疑虑(在空目录、空 xml 的情况下 - 不需要该列表),只需在静态声明本身中初始化 ListToWatch:

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

注意一个缺席的 setter:只有这个类被允许更改 listToWatch 引用。

于 2012-12-13T12:55:26.340 回答
1

您可以在静态构造函数中初始化 listToWatch,也可以使用 Gonzalo 建议的方法。

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();
            }
        }
    }
}
于 2012-12-13T12:50:27.220 回答
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.

于 2012-12-13T12:46:42.860 回答
0

你只需要创建一次这个列表,所以让它发生在 firstMethod 上。尝试隔离代码职责。

我认为@Gonzalo.- 解决方法对您的情况很有趣。

干杯

于 2012-12-13T12:50:12.510 回答