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あり、2 つを明確にする方法はありません。

EventArgsという名前のパラメーターを持つイベント ハンドラーにいる可能性が高く、識別子eの 1 つを別の名前に変更する必要があります。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. 無名関数 (ラムダ)

    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 ブロック) で再度宣言していることを意味します。Exception e を変更するException exceptと、うまくいくかもしれません。

于 2013-08-01T01:58:37.517 に答える