简体   繁体   English

尝试catch语句C#中的变量范围问题

[英]Variable scope problems in try catch statement c#

I hate to ask this simple of a question on here but I've researched this for a while and to no avail. 我不想在这里问这个简单的问题,但是我已经研究了一段时间,但无济于事。 And it's significantly limiting my application. 这极大地限制了我的应用程序。

Why am I getting a red squiggly on the excelWorksheet variable inside the try block (a local or parameter named 'excelWorksheet' cannot be declared in this scope because that name is used in an enclosing local scope). 为什么我在try块内的excelWorksheet变量上出现红色波浪状(在此范围内无法声明名为“ excelWorksheet”的本地或参数,因为该名称用于封闭的本地范围内)。 If I delete the null declaration, then all instances of 'excelWorksheet' after the try catch statement turn red. 如果我删除了null声明,那么try catch语句之后的所有'excelWorksheet'实例将变为红色。 Any help is extremely appreciated.Here's the code: 非常感谢您的帮助,以下是代码:

 Excel.Worksheet excelWorksheet = null;          
        try
        {
            //declare the worksheet variable raw data
            string currentSheet = "Raw Data";
            Excel.Worksheet excelWorksheet = (Excel.Worksheet)ExcelSheets.get_Item(currentSheet);
        }
        catch(Exception r)
        {
            MessageBox.Show("The Raw Data sheet in the report does not exist. " + r);
            return;
        }

a local or parameter named 'excelWorksheet' cannot be declared in this scope because that name is used in an enclosing local scope 不能在此范围内声明名为“ excelWorksheet”的本地或参数,因为该名称用于封闭的本地范围

You're trying to re-declare the variable. 您正在尝试重新声明该变量。 Remove the declaration and simply use the variable that was declared in the enclosing scope: 删除声明,仅使用包含在范围内的变量即可:

string currentSheet = "Raw Data";
excelWorksheet = (Excel.Worksheet)ExcelSheets.get_Item(currentSheet);

Could you try the below?. 您可以尝试以下吗?

Excel.Worksheet excelWorksheet = null;          
            try
            {
                //declare the worksheet variable raw data
                string currentSheet = "Raw Data";

//excelWorksheet variable already exists in the scope, so no need to declare it again // excelWorksheet变量已经存在于范围中,因此无需再次声明

                excelWorksheet = (Excel.Worksheet)ExcelSheets.get_Item(currentSheet);
            }
            catch(Exception r)
            {
                MessageBox.Show("The Raw Data sheet in the report does not exist. " + r);
                return;
            }
Excel.Worksheet excelWorksheet = null;          

with the above statement you already declare the excelWorksheet variable. 使用上面的语句,您已经声明了excelWorksheet变量。 You do not need to declare again. 您无需再次声明。 The code below: 下面的代码:

Excel.Worksheet excelWorksheet = null;          
            try
            {
                //declare the worksheet variable raw data
                excelWorksheet = ExcelSheets.get_Item("Raw Data") as Excel.Worksheet;
            }
            catch(Exception r)
            {
                MessageBox.Show("The Raw Data sheet in the report does not exist. " + r);
                return;
            }

Use as for casting. 用作铸造。 Its a good practice. 这是一个好习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM