变量名不能在此范围内声明
本文关键字:范围内 声明 不能 变量名 | 更新日期: 2023-09-27 18:06:21
我想到目前为止我已经一切正常了。如果本地变量"req"错误,则会留下一个错误…它已经在System.Net.WebRequest上面声明过了req = null;但我试图清除它之前使用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();
}
您已经在外部作用域中声明了它,因此您不能在内部作用域中重新声明它。不需要重新申报WebRequest
。删除两个声明中的一个。我将删除外部作用域中的那个,因为看起来您不需要在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();
}
变化:
WebRequest req = WebRequest.Create(strURL + "/book.php?qid=" + listId.SelectedItem as string);
:
req = WebRequest.Create(strURL + "/book.php?qid=" + listId.SelectedItem as string);
您不能在同一作用域中声明两个具有相同名称的局部变量,因此不使用WebRequest req = WebRequest.Create(....)
而使用req = WebRequest.Create(...)