简体   繁体   English

C#-在“封闭”局部范围内使用变量?

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

相关问题 使用C#-DisplayFormatAttribute中的属性格式化字段? - Formatting a field using attributes in C#- DisplayFormatAttribute? 在封闭的本地 scope 中使用名称来定义本地或参数 - Name is used in an enclosing local scope to define a local or parameter 使用SWIG代理C#-> C ++类包装器 - Proxying C#->C++ class wrappers using SWIG 不能在此 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 c# - HTTPS代理 - c#- HTTPS proxy C#- 使用 Microsoft 打印机打印现有 PDF - C#- Print Exisitng PDF using Microsoft Printer C#-“使用”语句似乎未被确认 - C#- 'using' statement doesn't appear to be acknowledged 局部变量范围 - Local variable scope
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM