2008-11-01 65 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(); 
    } 

错误即将到来,使用未分配的局部变量。