简体   繁体   English

变量名称不能在此范围内声明

[英]Variable name cannot be declared in this scope

I think I have everything working so far. 我想我到目前为止一切正常。 One error left if the local variable error for 'req' ... its already been declared above System.Net.WebRequest req = null; 如果'req'的局部变量错误已遗留一个错误...它已经在System.Net.WebRequest上面声明了req = null; but i'm trying to clear it before using for WebRequest req = WebRequest.Create... do I not need to do that? 但是我在尝试将其用于WebRequest req = WebRequest.Create之前将其清除...我不需要这样做吗?

while (listId.Items.Count > 0)
            {
                // making the first item as selected.
                listId.SelectedIndex = 0;

                foreach (object o in listProxy.Items)
                {
                    string strProxy = o as string;
                    WebProxy proxyObject = new WebProxy(strProxy, true); // insert listProxy proxy here
                    WebRequest.DefaultWebProxy = proxyObject;

                    string strURL = "http://www.zzzz.com"; // link from listId and insert here
                    System.Net.WebRequest req = null;

                    try
                    {
                        WebRequest req = WebRequest.Create(strURL + "/book.php?qid=" + listId.SelectedItem as string);
                        req.Proxy = proxyObject;
                        req.Method = "POST";
                        req.Timeout = 5000;

                    }
                    catch (Exception eq)
                    {
                        string sErr = "Cannot connect to " + strURL + " : " + eq.Message;
                        MessageBox.Show(sErr, strURL, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                }

                // remove the selected item.
                listId.Items.RemoveAt(0);

                // refreshing the list.
                listId.Refresh();
            }

You have already declared it in an outer scope, so you cannot re-declare it in an inner scope. 您已经在外部范围中声明了它,因此您不能在内部范围中重新声明它。 There is no need to re-declare the WebRequest . 无需重新声明WebRequest Remove one of the two declarations. 删除两个声明之一。 I would remove the one in the outer scope, since it looks like you don't need to reference it outside of the try block. 我将在外部范围中删除一个,因为看起来您不需要在try块之外引用它。

while (listId.Items.Count > 0)
{
    // making the first item as selected.
    listId.SelectedIndex = 0;

    foreach (object o in listProxy.Items)
    {
        string strProxy = o as string;
        WebProxy proxyObject = new WebProxy(strProxy, true); // insert listProxy proxy here
        WebRequest.DefaultWebProxy = proxyObject;

        string strURL = "http://www.zzzz.com"; // link from listId and insert here

        try
        {
            WebRequest req = WebRequest.Create(strURL + "/book.php?qid=" + listId.SelectedItem as string);
            req.Proxy = proxyObject;
            req.Method = "POST";
            req.Timeout = 5000;
        }
        catch (Exception eq)
        {
            string sErr = "Cannot connect to " + strURL + " : " + eq.Message;
            MessageBox.Show(sErr, strURL, MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }
    }

    // remove the selected item.
    listId.Items.RemoveAt(0);

    // refreshing the list.
    listId.Refresh();
}

Change: 更改:

WebRequest req = WebRequest.Create(strURL + "/book.php?qid=" + listId.SelectedItem as string);

To: 至:

   req = WebRequest.Create(strURL + "/book.php?qid=" + listId.SelectedItem as string);

您不能在相同范围内声明两个具有相同名称的局部变量,因此不要使用WebRequest req = WebRequest.Create(....)使用req = WebRequest.Create(...)

暂无
暂无

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

相关问题 为什么我不能声明具有已经声明的相同名称的变量,但是新变量超出了其他变量的范围 - Why I cannot declare the variable with same name that already declared but new variable is out of the scope of other variable 无法在此范围内声明名为“ myList”的局部变量 - local variable named 'myList' cannot be declared in this scope 错误无法在此范围内声明局部变量 - Error A local variable cannot be declared in this scope 不能在此 scope 中声明名为“turnContext”的本地或参数,因为该名称 - A local or parameter named 'turnContext' cannot be declared in this scope because that name 错误:无法在此 scope 中声明名为“Model”的局部变量 - Error:A local variable named 'Model' cannot be declared in this scope 为什么这个错误“局部变量不能在这个范围内声明,因为它已经被使用了”? - Why is this error “local variable cannot be declared in this scope because it … is already used”? 不能在此范围内声明局部变量[Linq/Lambda 表达式] - A local variable cannot be declared in this scope[Linq/Lambda expression] 不能在此 scope 中声明名为“nextjunction”的本地或参数,因为该名称用于封闭本地 scope 以定义本地 - A local or parameter named 'nextjunction' cannot be declared in this scope because that name is used in an enclosing local scope to define a local 无法在此范围内声明名为“ g”的本地或参数,因为该名称在封闭的本地范围内用于定义本地或参数 - A local or parameter named 'g' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter 不能在此 scope 中声明名为“student”的本地或参数,因为该名称用于封闭的本地 scope 以定义本地参数 - A local or parameter named 'student' cannot be declared in this scope because that name is used in an enclosing local scope to define a local param
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM