I am trying to write a simple static class state machine for my application to notify other controls and code when the system state changes. And I think that I have almost none, but I have a small problem, and I'm not sure how to work.
Here is the code:
// An enum denoting the 3 States public enum Status { Error = -1, Working, Ready } // The main state change class public static class Sys { // system status private static Status state; // delegate and event public static delegate void StateChangeHandler(object sys, SysInfoEventArgs sysStateInfo); public static event StateChangeHandler OnStateChange; public static Status State { get { return state; } set { SysInfoEventArgs sysInfo = new SysInfoEventArgs(state, value); state = value; OnStateChange(this, sysInfo); } } } /// <summary>Contains previous and current state info</summary> public class SysInfoEventArgs : EventArgs { public readonly Status oldState; public readonly Status newState; public SysInfoEventArgs(Status oldState, Status newState) { this.oldState = oldState; this.newState = newState; } }
The problem I encountered is related to this line:
OnStateChange(this, sysInfo);
In particular, the word "this" is illegal. And I understand why: "this" should reference the self of the instance object (and not the static class).
I would prefer the Static class for my state machine, rather than one instance of multiple instances. (Not that it was so bad, but I feel like it makes the code cleaner a static class.)
So how should I work?
Update:
As a continuation, I chose Jon Skeet as the right one, because the problem was more about what approach I took, rather than the technical failure that I had. Although, almost all the other answers below determine the technical malfunction I was dealing with.
Oddly enough, as I examined the application that I wrote with my colleague, she indicated that the program should probably monitor the status of the connection to the server, as well as the status of the work being performed. (Yes, Virginia, that means I need two state machines ... Ergo, removing all the βstaticβ keywords from the code above and making it a regular class is a smart approach.)
Thanks again everyone!