2013-02-28 583 views
1

有人可以請解釋爲什麼我得到:C#局部變量

「使用未分配的局部變量number_of_column」 爲:如果(我< number_of_column -1)

,什麼是最好的方式處理這個?

static void Main(string[] args) 
{ 
    int number_of_column; 
    if (Directory.Exists(path)) 
    { 
     var file = dir.GetFiles().OrderByDescending(f => f.LastWriteTime).First(); 
     string file1 = file.ToString(); 
     Console.WriteLine(file1); 
     StreamReader sr = new StreamReader(path + "\\" + file1); 
     string line; 
     while ((line = sr.ReadLine()) != null) 
     { 
      if (start == true) 
      { 
       string[] line1 = line.Split(','); 
       number_of_column = line1.Count(); 
       i = 0; 
       foreach (string s in line1) 
       { 
        if ((s != "0") || (!string.IsNullOrEmpty(s))) 
        { 
         col[i] = "checked"; 

        } 
        i++; 
       } 
      } 
      else 
      { 
       if (line.Contains("Timestamp") && line.Contains("LiveStandby") && line.Contains("peak")) 
       { 
        start = true; 

       } 
      } 
     } 
     sr.Close(); 

     i=0; 
     foreach (string s in col) 
     { 
      if (i < number_of_column -1) 
      { 
+0

想想爲什麼你聲明'number_of_column'都將是您解決問題的第一步。 – 2013-02-28 17:48:06

回答

7

如果你從來沒有進入while循環,該變量沒有分配的值。

如果你點擊if中的else分支,變量沒有賦值。

這些是你得到這個錯誤信息的原因。

您可以通過在聲明變量時分配值來解決此問題。

C#要求您在使用它們之前初始化局部變量。 成員變量,這是沒有必要的,他們得到他們的默認值自動分配。

+0

@MarcinJuraszek它看起來像在它之後的foreach循環中 – 2013-02-28 16:50:35

0

試着改變你的第一行:

static void Main(string[] args){ int number_of_column = 0;if (Directory.Exists(path)) 

或格式化:

static void Main(string[] args) 
{ 
    int number_of_column = 0; 
    if (Directory.Exists(path)) ... 
0

這意味着你沒有給予任何初始值到它。

替換此

int number_of_column; 

隨着

int number_of_column = 0; 

這將解決您的問題。

0

作爲一種防止副作用的機制,C#不允許使用未初始化的變量 - 聲明的變量,但沒有明確設置的值。這就是你得到錯誤的原因。初始化爲零可以解決問題。

+0

是的,在C#中,它是必需的。 – 2013-02-28 16:56:20

2

完全精煉代碼的

  1. file然後file1只是多餘的,語句可以合併和被更好的語義含義。

  2. if (start == true)過於複雜,只是if(start)

  3. 使用using Statement woule更好地確保像StreamReader的正確使用IDisposable

  4. Path.Combine Method將兩個字符串組合成一個路徑。

  5. 這兩個foreach s最好是for,因爲你是按順序迭代數組。

  6. 不難發現number_of_column不是必要的,因爲您只是存儲數組迭代的計數。


代碼:

static void Main(string[] args) { 
    int number_of_column; // never used 

    if(Directory.Exists(path)) { 
     var file1=(
      from f in dir.GetFiles() 
      orderby f.LastWriteTime 
      select f 
      ).First().ToString(); 

     Console.WriteLine(file1); 

     using(var sr=new StreamReader(Path.Combine(path, file1))) 
      for(String line; null!=(line=sr.ReadLine());) { 
       if(start) { 
        var line1=line.Split(','); 

        for(var i=0; i<line1.Length; ++i) { 
         var s=line1[i]; 

         if("0"!=s||!String.IsNullOrEmpty(s)) 
          col[i]="checked"; 
        } 

        continue; 
       } 

       if(
        line.Contains("Timestamp") 
        && 
        line.Contains("LiveStandby") 
        && 
        line.Contains("peak" 
        )) 
        start=true; 
      } 

     for(var i=0; i<col.Length; ++i) { 
      // following lines are no more needed 
      // if(i<number_of_column-1) { 
      // } 
     } 
    } 
}