[英]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.