if 语句中的变量声明
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果
bla1Window
和bla2Window
都共享基类或接口,您可以通过这种方式引用它们。在这种情况下,看起来您只是访问Window
的属性,因此您可以执行以下操作:If
bla1Window
andbla2Window
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 ofWindow
, so you could do:考虑一下:
我通常会这样写,但是:
快乐编码。
Consider:
I would generally write it similar to this, however:
Happy coding.
解决方案是简单地将变量提升到 if 语句之后使用所需的范围。真的就是这么简单。不过,我建议您尝试重构它,或者至少发布您的真实代码,以便我们可以尝试一下。当你有一堆代码在多个 if 语句中一个接一个地重复时,通常可以将其简化为一个或两个方法。
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.
您的所有窗口都应该有一个共享的父窗口。使用子构造函数并将其分配给可以在“if”语句之外单独声明的父对象。
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.
您确实希望将其设为一个接口,并在
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 onbla
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.
我认为你应该确保在 if 语句之前声明变量。
那应该可以解决你的问题。
例子
I think you should just make sure you declare the variable before the if statement.
That should solve your problem.
Example