if 语句中的变量声明

发布于 2024-12-02 10:07:26 字数 619 浏览 1 评论 0原文

在 C# 中,对于我正在编写的一个相对简单的程序,我试图创建一个事件处理函数来处理多个源,如下所示:

private void fooHandler(object sender, RoutedEventArgs e)
{
    fooObject objectFoo = (fooObject)sender;
    if (objectFoo.name == "bla1"){
        bla1Window bla = new bla1Window();
    }
    if (objectFoo.name == "bla2"){
        bla2Window bla = new bla2Window();
    }
    .
    .
    .
    else{
        //default stuff happens
    }
bla.Left = this.Left
bla.Top = this.Top
bla.Show();
this.Close();
}

该函数用于窗口切换。 问题是,一旦我退出 if 语句,变量就会超出范围。我这样做是因为,查看我定义的一系列函数来单独处理每个事件,除了一个变量声明之外,它们都是相同的。有没有办法做到这一点,或者我是否必须坚持为每个事件处理程序使用一个函数?

In C# for a relatively simple program I am writing I am trying to create an event handler function that will handle multiple sources, like so:

private void fooHandler(object sender, RoutedEventArgs e)
{
    fooObject objectFoo = (fooObject)sender;
    if (objectFoo.name == "bla1"){
        bla1Window bla = new bla1Window();
    }
    if (objectFoo.name == "bla2"){
        bla2Window bla = new bla2Window();
    }
    .
    .
    .
    else{
        //default stuff happens
    }
bla.Left = this.Left
bla.Top = this.Top
bla.Show();
this.Close();
}

The function is for window switching.
The problem is the variable falls out of scope as soon as I exit the if-statement. I'm doing it this way because, looking at the series of functions I've defined to handle each event individually, they are all the same with the exception of the one variable declaration. Is there a way to make this work, or am I just going to have to stick with a function for each event handler?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

半葬歌 2024-12-09 10:07:27

如果 bla1Windowbla2Window 都共享基类或接口,您可以通过这种方式引用它们。在这种情况下,看起来您只是访问 Window 的属性,因此您可以执行以下操作:

Window window = null;
fooObject objectFoo = (fooObject)sender;
if (objectFoo.name == "bla1"){
    window = new bla1Window();
}
else if (objectFoo.name == "bla2"){
    window = new bla2Window();
}
.
.
.
else{
    //default stuff happens
}

window.Left = this.Left
window.Top = this.Top
window.Show();
this.Close();

If bla1Window and bla2Window both share a base class or interface, you can refer to them that way. In this case, it looks like you're just accessing properties of Window, so you could do:

Window window = null;
fooObject objectFoo = (fooObject)sender;
if (objectFoo.name == "bla1"){
    window = new bla1Window();
}
else if (objectFoo.name == "bla2"){
    window = new bla2Window();
}
.
.
.
else{
    //default stuff happens
}

window.Left = this.Left
window.Top = this.Top
window.Show();
this.Close();
南巷近海 2024-12-09 10:07:27

考虑一下:

private void fooHandler(object sender, RoutedEventArgs e)
{
    fooObject objectFoo = (fooObject)sender;
    Window bla; // a super-type or interface, don't assign a value here
                // so there will be a compile error if it was
                // forgotten below
    if (objectFoo.name == "bla1"){
        bla = new bla1Window();
    } else if (objectFoo.name == "bla2"){
        bla = new bla2Window();
    } else {
        // just make sure to assign to bla
        // or there will a compiler error later
    }
    bla.Left = this.Left
    bla.Top = this.Top
    bla.Show();
    this.Close();
}

我通常会这样写,但是:

Window CreateFromName(string name) {
    if (name == "bla1"){
        return new bla1Window();
    } else if (name == "bla2"){
        return new bla2Window();
    } else {
        // just make sure to return a value
        // or there will a compiler error later
    }
}

private void fooHandler(object sender, RoutedEventArgs e)
{
    fooObject objectFoo = (fooObject)sender;
    Window bla = CreateFromName(objectFoo.name);
    bla.Left = this.Left
    bla.Top = this.Top
    bla.Show();
    this.Close();
}

快乐编码。

Consider:

private void fooHandler(object sender, RoutedEventArgs e)
{
    fooObject objectFoo = (fooObject)sender;
    Window bla; // a super-type or interface, don't assign a value here
                // so there will be a compile error if it was
                // forgotten below
    if (objectFoo.name == "bla1"){
        bla = new bla1Window();
    } else if (objectFoo.name == "bla2"){
        bla = new bla2Window();
    } else {
        // just make sure to assign to bla
        // or there will a compiler error later
    }
    bla.Left = this.Left
    bla.Top = this.Top
    bla.Show();
    this.Close();
}

I would generally write it similar to this, however:

Window CreateFromName(string name) {
    if (name == "bla1"){
        return new bla1Window();
    } else if (name == "bla2"){
        return new bla2Window();
    } else {
        // just make sure to return a value
        // or there will a compiler error later
    }
}

private void fooHandler(object sender, RoutedEventArgs e)
{
    fooObject objectFoo = (fooObject)sender;
    Window bla = CreateFromName(objectFoo.name);
    bla.Left = this.Left
    bla.Top = this.Top
    bla.Show();
    this.Close();
}

Happy coding.

新人笑 2024-12-09 10:07:27

解决方案是简单地将变量提升到 if 语句之后使用所需的范围。真的就是这么简单。不过,我建议您尝试重构它,或者至少发布您的真实代码,以便我们可以尝试一下。当你有一堆代码在多个 if 语句中一个接一个地重复时,通常可以将其简化为一个或两个方法。

private void fooHandler(object sender, RoutedEventArgs e)
{
    fooObject objectFoo = (fooObject)sender;

    // use the base class and work with that.
    // all windows have the properties you use 
    // below, so there is no need to declare it
    // as a more specific type.
    blahWindow bla = null; 
    if (objectFoo.name == "bla1"){
        bla = new bla1Window();
    }
    if (objectFoo.name == "bla2"){
        bla = new bla2Window();
    }
    .
    .
    .
    else{
        //default stuff happens
        bla = new BlahDefault();
    }

    // 'bla' cannot be nbull here if each branch above assigns it
    bla.Left = this.Left
    bla.Top = this.Top
    bla.Show();
    this.Close();
}

The solution is to simply hoist the variable into the scope it needs to be in to be used after the if statement(s). It's really that simple. I would however suggest you take a stab at refactoring this, or at least post your real code so we can give it a shot. When you have a bunch of code that is repeated in multiple if statements one after another like that it can usually be simplified into a single method or two.

private void fooHandler(object sender, RoutedEventArgs e)
{
    fooObject objectFoo = (fooObject)sender;

    // use the base class and work with that.
    // all windows have the properties you use 
    // below, so there is no need to declare it
    // as a more specific type.
    blahWindow bla = null; 
    if (objectFoo.name == "bla1"){
        bla = new bla1Window();
    }
    if (objectFoo.name == "bla2"){
        bla = new bla2Window();
    }
    .
    .
    .
    else{
        //default stuff happens
        bla = new BlahDefault();
    }

    // 'bla' cannot be nbull here if each branch above assigns it
    bla.Left = this.Left
    bla.Top = this.Top
    bla.Show();
    this.Close();
}
单身狗的梦 2024-12-09 10:07:27

您的所有窗口都应该有一个共享的父窗口。使用子构造函数并将其分配给可以在“if”语句之外单独声明的父对象。

private void fooHandler(object sender, RoutedEventArgs e)
{
    Window bla = null;
    fooObject objectFoo = (fooObject)sender;
    if (objectFoo.name == "bla1"){
        bla = new bla1Window();
    }
    if (objectFoo.name == "bla2"){
        bla = new bla2Window();
    }
    .
    .
    .
    else{
        //default stuff happens
    }
    if(bla != null)
    {
        bla.Left = this.Left
        bla.Top = this.Top
        bla.Show();
        this.Close();
    }
}

All your windows should have a shared parent. Use the child constructor and assign it to the parent object which can be singly declared outside of the 'if' statement.

private void fooHandler(object sender, RoutedEventArgs e)
{
    Window bla = null;
    fooObject objectFoo = (fooObject)sender;
    if (objectFoo.name == "bla1"){
        bla = new bla1Window();
    }
    if (objectFoo.name == "bla2"){
        bla = new bla2Window();
    }
    .
    .
    .
    else{
        //default stuff happens
    }
    if(bla != null)
    {
        bla.Left = this.Left
        bla.Top = this.Top
        bla.Show();
        this.Close();
    }
}
秉烛思 2024-12-09 10:07:27

您确实希望将其设为一个接口,并在 if 代码之前声明该接口。看起来下面的 bla 上调用的所有方法都是通用的,这是接口(或抽象类,如果更合适的话)的一个很好的候选者。

事实上,如果您的代码根本不在这个文件中切换,并且您在工厂或其他东西内部通过它,那就最好了。如果您决定走这条路,互联网上有很多相关信息。

You really want to make this an interface and declare the interface before the if code. It looks like all of the methods that are called on bla below are common which is a great candidate for an interface (or abstract class if that is more appropriate).

In fact, it would be best if your code didn't switch at all in this file and you through it inside of a Factory or something. There is a lot of information on the internet about that if you decide to go that route.

追风人 2024-12-09 10:07:27

我认为你应该确保在 if 语句之前声明变量。
那应该可以解决你的问题。
例子

public string IfStatement()
{   
    string myValue = null;

    bool condition = true;
    if (condition)
    {
        myValue  = "something";
    }
    else
    {
        myValue  = "something else";
    }
    return myValue;
}

I think you should just make sure you declare the variable before the if statement.
That should solve your problem.
Example

public string IfStatement()
{   
    string myValue = null;

    bool condition = true;
    if (condition)
    {
        myValue  = "something";
    }
    else
    {
        myValue  = "something else";
    }
    return myValue;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文