Reputation: 353
In the same class, I have :
public partial class MainWindow : Window {
// event handler
private static void messageReceived (object sender, messageReceivedEvent args)
{
//some code
stopProcesses()
}
void stopProcesses()
{
//some code
}
}
In the event handler, the call to stopProcesses() give the error in the title : an object reference is required for the non static field, method or property 'MainWindow.stopProcesses'. They're in the same class, and I can't have stopProcesses as static because I'd have to have every variable and every method if the class as static and that's not the point. I don't know how to call the non static method from a static event handler though, what's an object reference to the method ?
Upvotes: 1
Views: 289
Reputation: 62308
The best solution is to make messageReceived
instance scoped instead of static.
private void messageReceived (object sender, messageReceivedEvent args) {}
If you want to keep it static then you have to use the sender
instance which should be a reference back to your MainWindow
instance, all you have to do is cast it.
private static void messageReceived (object sender, messageReceivedEvent args)
{
((MainWindow)sender).stopProcesses();
}
Upvotes: 4