2008-11-01 42 views
28

我不知道爲什麼我得到這個錯誤,但不應該編譯此代碼,因爲我已經檢查,看看隊列是否已初始化?C#錯誤:使用未分配的本地變量

public static void Main(String[] args) 
{ 
    Byte maxSize; 
    Queue queue; 

    if(args.Length != 0) 
    { 
     if(Byte.TryParse(args[0], out maxSize)) 
      queue = new Queue(){MaxSize = maxSize}; 
     else 
      Environment.Exit(0); 
    } 
    else 
    { 
     Environment.Exit(0); 
    } 

    for(Byte j = 0; j < queue.MaxSize; j++) 
     queue.Insert(j); 
    for(Byte j = 0; j < queue.MaxSize; j++) 
     Console.WriteLine(queue.Remove()); 
} 

所以如果隊列沒有初始化,那麼for循環是不可達的嗎?由於程序已經以Environment.Exit(0)結束?

希望你們大家能給我一些指點:)

感謝。

+6

我不能給你任何指示,但我希望你能處理這一個。 – wprl 2008-11-01 20:40:31

回答

73

編譯器不知道Environment.Exit()將要終止程序;它只是看到你在一個類上執行靜態方法。當你聲明它時,初始化queue爲null。

Queue queue = null; 
9

編譯器不知道Environment.Exit()不返回。爲什麼不直接從Main()返回?

+0

如果從腳本調用程序,我更喜歡使用具有非零錯誤狀態的Environment.Exit。這樣腳本可以通過檢查退出狀態來知道程序是否成功。 – tvanfosson 2008-11-01 20:51:06

+4

將main的返回類型更改爲int並返回狀態。 – 2008-12-05 01:18:17

0

編譯器只知道代碼或者使用「重返」不可達。把Environment.Exit()想象成你調用的函數,編譯器不知道它會關閉應用程序。

+0

我可以考慮其他兩個無法訪問的代碼來源: 中斷或繼續循環,並拋出:) – 2008-11-02 00:55:07

8

解決此問題的幾種不同方法:

只需將Environment.Exit替換爲return即可。編譯器知道返回結束該方法,但不知道Environment.Exit會。

static void Main(string[] args) { 
    if(args.Length != 0) { 
     if(Byte.TryParse(args[0], out maxSize)) 
     queue = new Queue(){MaxSize = maxSize}; 
     else 
     return; 
    } else { 
     return; 
} 

當然,你真的只能逃避,因爲你在所有情況下使用0作爲退出代碼。真的,你應該返回一個int而不是使用Environment.Exit。對於這種特殊的情況下,這將是我的首選方法

static int Main(string[] args) { 
    if(args.Length != 0) { 
     if(Byte.TryParse(args[0], out maxSize)) 
     queue = new Queue(){MaxSize = maxSize}; 
     else 
     return 1; 
    } else { 
     return 2; 
    } 
} 

初始化隊列爲空,這其實只是一個編譯器把戲,說:「我會找出我自己未初始化的變量,非常感謝你。」這是一個很有用的技巧,但在這種情況下我不喜歡它 - 如果分支太多,很容易檢查你是否正確地做了這件事。如果你真的做這種方式,這樣的事情會更清楚:

static void Main(string[] args) { 
    Byte maxSize; 
    Queue queue = null; 

    if(args.Length == 0 || !Byte.TryParse(args[0], out maxSize)) { 
    Environment.Exit(0); 
    } 
    queue = new Queue(){MaxSize = maxSize}; 

    for(Byte j = 0; j < queue.MaxSize; j++) 
    queue.Insert(j); 
    for(Byte j = 0; j < queue.MaxSize; j++) 
    Console.WriteLine(queue.Remove()); 
} 

添加return語句Environment.Exit後。再次,這更是一個編譯器的伎倆 - 但稍微合法IMO,因爲它增加了語義人以及(儘管它會保護你從吹噓的100%的代碼覆蓋率)

static void Main(String[] args) { 
    if(args.Length != 0) { 
    if(Byte.TryParse(args[0], out maxSize)) { 
     queue = new Queue(){MaxSize = maxSize}; 
    } else { 
     Environment.Exit(0); 
     return; 
    } 
    } else { 
    Environment.Exit(0); 
    return; 
    } 

    for(Byte j = 0; j < queue.MaxSize; j++) 
    queue.Insert(j); 
    for(Byte j = 0; j < queue.MaxSize; j++) 
    Console.WriteLine(queue.Remove()); 
} 
-4

的SqlConnection CON; SqlCommand com; con = new SqlConnection(「Data Source =。\ SQLEXPRESS; AttachDbFilename =」+ Server.MapPath(「〜\ App_Data \ Database.mdf」)+「; Integrated Security = True; User Instance = True」); con.Open();

com = new SqlCommand("insert into blog values(@b)", con); 
    //com.Parameters.AddWithValue("@a", TextBox1.Text); 
    com.Parameters.AddWithValue("@b",TextBox2.Text); 
    com.ExecuteNonQuery(); 
    con.Close(); 
    TextBox1.Visible = true; 
    SqlDataReader DR1; 

    while (DR1.Read()) 
    { 
     TextBox1.Text = DR1[1].ToString(); 
    } 

錯誤即將到來,使用未分配的局部變量。

相關問題