0

当我运行此代码时,我在 catch(异常 e)部分出现错误,我不知道为什么,编译器说“不能在此范围内声明名为 'e' 的局部变量,因为它会给 ' 赋予不同的含义e',已在“父级或当前”范围中用于表示其他内容”

        try
        {

            //Form Query which will insert Company and will output generated id 
            myCommand.CommandText = "Insert into Comp(company_name) Output Inserted.ID VALUES (@company_name)";
            myCommand.Parameters.AddWithValue("@company_name", txtCompName);
            int companyId = Convert.ToInt32(myCommand.ExecuteScalar());

            //For the next scenario, in case you need to execute another command do it before committing the transaction

            myTrans.Commit();

            //Output Message in message box
            MessageBox.Show("Added", "Company Added with id" + companyId, MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        catch (Exception e)
        {
            try
            {
                myTrans.Rollback();
            }
            catch (SqlException ex)
            {
                if (myTrans.Connection != null)
                {
                    MessageBox.Show("An exception of type " + ex.GetType() +
                                      " was encountered while attempting to roll back the transaction.");
                }
            }

            MessageBox.Show("An exception of type " + e.GetType() +
                              "was encountered while inserting the data.");
            MessageBox.Show("Record was written to database.");

        }
        finally
        {
            myConnection.Close();
        }

希望得到您的回复!谢谢!

4

2 回答 2

4

您在本地范围内的其他地方有一个变量e,并且无法消除两者之间的歧义。

很可能您在一个带有EventArgs命名参数的事件处理程序中e,您应该将其中一个e标识符重命名为其他名称。

以下示例演示了此问题:

  1. 参数名称冲突

    void MyEventHandler(object source, EventArgs e)
    //                                          ^^^
    {
        try
        {
            DoSomething();
        }
        catch (Exception e)
        //              ^^^
        {
            OhNo(e);
            // Which "e" is this? Is it the Exception or the EventArgs??
        }
    }
    
  2. 冲突的局部变量

    void MyMethod()
    {
        decimal e = 2.71828;
        //     ^^^
    
        try
        {
            DoSomething();
        }
        catch (Exception e)
        //              ^^^
        {
            OhNo(e);
            // Which "e" is this? Is it the Exception or the Decimal??
        }
    }
    
  3. 匿名函数 (lambda)

    void MyMethod()
    {
        decimal e = 2.71828;
        //     ^^^
    
        var sum = Enumerable.Range(1, 10)
                            .Sum(e => e * e); //Which "e" to multiply?
        //                      ^^^
    }
    

请注意,以下内容不会导致相同的错误,因为您可以使用this关键字消除歧义:

class MyClass
{
    int e;

    void MyMethod()
    {
        try
        {
            DoSomething(e); //Here it is the Int32 field
        }
        catch (Exception e)
        {
            OhNo(e); //Here it is the exception
            DoSomethingElse(this.e); //Here it is the Int32 field
        }
    }

}
于 2013-08-01T01:58:08.227 回答
0

这意味着您之前已经声明了一个名为 e 的变量,现在在同一个代码块中,或者在它内部的一个块(这个 try/catch 块)中,您再次声明它。将异常 e 更改为Exception except它可能会起作用。

于 2013-08-01T01:58:37.517 回答