2011-11-27 81 views
2

我无法确定为什么这里的代码不能在使用Mono 2.10.6的Win32上的MonoDevelop 2.8.2中编译。 Monodevelop表明found_image_paths是一个未分配的局部变量?为什么这个变量在C#中通过Mono被列为未分配的局部变量

我失去了一些东西在这里?我是C新手#

string path = this.DirectoryChooser.CurrentFolder; 

    Console.WriteLine ("Selected Path: " + path); 

    //Iterate over all DAE files in this folder 
    string[] model_paths = Directory.GetFiles(path,"*.dae"); 
    HashSet<string> found_image_paths; 

    foreach (string dae_path in model_paths) 
    { 

     XmlDocument xmlDoc= new XmlDocument(); //* create an xml document object. 

     xmlDoc.Load(dae_path); //* load the XML document from the specified file. 

     //* Get elements. 
     XmlNodeList library_images = xmlDoc.GetElementsByTagName("library_images"); 

     System.Console.WriteLine(dae_path); 
     foreach (XmlNode image_node in library_images[0].ChildNodes) { 
      string image_path = image_node.FirstChild.InnerText; 
      found_image_paths.Add(image_path); 
      Console.WriteLine(image_path); 
     } 



    } 
    //The next line returns the error "Use of unassigned local variable 'found_image_paths' 
    foreach (var item in found_image_paths) { 

回答

4

因为它是未分配的;您需要实例化一个哈希集并将其分配给您的变量或至少将其分配给null

2

这是正确的。你需要初始化它。

+2

'HashSet found_image_paths = new HashSet ();'似乎工作。 – JonnyRo

相关问题