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
bla1Windowandbla2Windowboth 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: