开发者

Variable declaration in an if statement

开发者 https://www.devze.com 2023-04-01 03:38 出处:网络
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:

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?


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();


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.


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();
}


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();
    }
}


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.


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号