2011-11-27 52 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

相關問題