开发者

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; but i'm trying to clear it before using for WebRequest req = WebRequest.Create... do I not need to do that?

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

                foreach (object o in listProxy.Items)
                {
         开发者_如何学Python           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. 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.

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);


You can't declare two local variables with the same name within same scope, so instead of using WebRequest req = WebRequest.Create(....) use req = WebRequest.Create(...)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜