2015-10-14 77 views
-6

這段代碼的工作原理,但我不明白其背後的原因(它只是程序的一部分)我的主要關注點是如何填寫我的字典讀取方法?我沒有使用任何參考,但不知何故無效方法返回一個值,它是如何可能的?爲什麼我的字典沒有被返回

我正在閱讀3個不同的文件。不知何故,他們都放在單個字典中,有人能告訴我確切的行嗎?

 Dictionary<string, Atstovybe> Atstovybes = new Dictionary<string, Atstovybe>(); 

     Atstovybes.Add("InfoSA", new Atstovybe("InfoSA")); 
     Atstovybes.Add("Statius", new Atstovybe("Statius")); 
     Atstovybes.Add("VfSA", new Atstovybe("VfSA")); 

     string[] filepaths = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.txt"); 

     foreach (string file in filepaths) 
     { 
      SkaitytiDuomenis(file, Atstovybes); 
     } 


    public static void SkaitytiDuomenis(string file, Dictionary<string, Atstovybe> Atstovybes) 
    { 

     string AtstovybePav = null; 

     using (StreamReader reader = new StreamReader(@file)) 
     { 
      string line = null; 
      line = reader.ReadLine(); 
      if (line != null) 
      { 
       AtstovybePav = line; 
      } 

      Atstovybe atstovybe = Atstovybes[AtstovybePav]; 

      while (null!= (line=reader.ReadLine())) 
      { 
       string[] values = line.Split(';'); 

       string tema = values[0]; 
       int sudetingumas = int.Parse(values[1]); 
       string autorius = values[2]; 
       string tekstas = values[3]; 
       string var1 = values[4]; 
       string var2 = values[5]; 
       string var3 = values[6]; 
       string var4 = values[7]; 
       string teisingas = values[8]; 
       int balai = int.Parse(values[9]); 

       Klausimas klausimas = new Klausimas(tema, sudetingumas, autorius, tekstas, var1, var2, var3, var4, teisingas, balai); 

       atstovybe.Klausimai.Add(klausimas); 

      } 


     } 

    } 
+1

將您的代碼添加到您的問題中,而不是鏈接到第三方網站。它提高了在搜索中找到它的能力。 –

+5

聽起來像你不明白參考類型如何在.NET中工作 - 我建議你閱讀http://jonskeet.uk/csharp/references.html和http://jonskeet.uk/csharp/parameters.html –

+0

謝謝神。至少你沒有把它當作bug。 –

回答

-2

atstovybe.Klausimai.Add(klausimas); 是在SkaitytiDuomenis方法,這是通過參考值添加值。

+3

在這裏沒有「通過參考」。 「通過參考」與「參考文獻的行爲」相結合對IMO沒有幫助。 –

+0

如果你看SkaitytiDuomenis(文件,Atstovybes);在這裏你傳遞字典的對象,這是字典的參考。希望這會有所幫助。 –

+2

是的,這是一個參考。我明白那個。但是使用術語「by ref」是* not *有用,因爲它將'ref'參數與參考類型混淆。 –

相關問題