[英]C#- using a variable in an 'enclosing' local scope?
I am trying to add some new features to a C# application- in particular, trying to replicate some of its behavior, but inside a web browser, rather than in the application, as it currently is. 我正在尝试向C#应用程序中添加一些新功能,特别是尝试复制其某些行为,但是要在Web浏览器中而不是在当前的应用程序中进行复制。
I am trying to call a method that has been defined in the Browser.cs
class from inside a method in the MainWindow.cs
class. 我正在尝试从MainWindow.cs
类的方法内部调用在Browser.cs
类中定义的方法。
The method is defined in Browser.cs
with: 该方法在Browser.cs
定义为:
public partial class Browser : Form{
public Browser(){
...
}
public void Browser_Load(object sender, EventArgs e){
webKitBrowser1.Navigate("https://google.com");
}
...
}
I am then trying to call it from MainWindow.cs
as follows: 然后,我尝试从MainWindow.cs
调用它,如下所示:
public partial class MainWindow : Window{
...
public MainWindow(){
...
Browser mBrowser = new Browser();
Object sender = new Object();
EventArgs e = new EventArgs();
mBrowser.Browser_Load(sender, e);
...
}
...
}
But, I'm getting a compile error that says: 但是,我收到一个编译错误,指出:
A local or parameter named 'e' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter 不能在此范围内声明名为“ e”的本地或参数,因为该名称在封闭的本地范围内用于定义本地或参数
What does this mean? 这是什么意思? I've never come across this error before- I am using the variable inside the same scope as where it has been declared- what does it mean by 'enclosing local scope'? 我以前从未遇到过此错误-我在与声明位置相同的作用域内使用变量-“封闭局部作用域”是什么意思? Is that because I'm using e
inside the parenthesis for the method call to mBrowser.Browser_Load(sender, e)
? 那是因为我使用e
括号内的方法调用mBrowser.Browser_Load(sender, e)
Surely, since the call to this method is inside the same scope as where I've defined e
, it shouldn't be an issue of scope? 当然,由于对该方法的调用与我定义e
作用域相同,因此它不应该成为作用域的问题吗?
I did try performing the call with: 我确实尝试通过以下方式执行呼叫:
mBrowser.Browser_Load(sender, EventArgs e);
but this gave me a compile error saying: 但这给了我一个编译错误:
'EventArgs' is a type, which is not valid in the given context. 'EventArgs'是一种类型,在给定的上下文中无效。
Can anyone point out what I'm doing wrong here, and what I should be doing to be able to call this method correctly? 谁能指出我在这里做错了什么,以及应该如何才能正确调用此方法?
The error is pretty clear, you have already defined e
named variable in your scope, (Probably in the part of code that you haven't shown) . 错误很明显,您已经在范围内定义了e
命名变量(可能在未显示的代码部分) 。
But more importantly, you shouldn't be calling the Load
event like that, instead extract the functionality in a separate method and call the method from your Load
event and other places. 但更重要的是,您不应该这样调用Load
事件,而应该在单独的方法中提取功能,并从Load
事件和其他地方调用该方法 。
Like: 喜欢:
public void SomeMethodToBeCalledOnLoad()
{
webKitBrowser1.Navigate("https://google.com");
}
public void Browser_Load(object sender, EventArgs e)
{
SomeMethodToBeCalledOnLoad();
}
public MainWindow(){
...
Browser mBrowser = new Browser();
Object sender = new Object();
EventArgs e = new EventArgs();
SomeMethodToBeCalledOnLoad();//here
...
}
You already have a variable named e
in the scope. 您已经在作用域中有一个名为e
的变量。
try to call your method like this 尝试像这样调用您的方法
mBrowser.Browser_Load(this, EventArgs.Empty);
and the error should go. 错误应该消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.