Sounds like a job for listings!
enum YourEnum { DoThis, DoThat } YourEnum foo = (YourEnum)yourInt;
Visual studio can even create an entire switch statement using inline snippets, and your code will become very readable.
switch(foo)
becomes
switch(foo) { case YourEnum.DoThis: break; case YourEnum.DoThat: break; default: break; }
Update 1
It's a little scary in terms of maintainability, but if you created a class like:
public class ActionProcessor { public void Process(int yourInt) { var methods = this.GetType().GetMethods(); if (methods.Length > yourInt) { methods[yourInt].Invoke(this, null); } } public DoThis() { } public DoThat() { }
or a little nicer, but harder to maintain:
[AttributeUsageAttribute(AttributeTargets.Method, Inherited = false, AllowMultiple = false)] public sealed class AutoActionAttribute : Attribute { public AutoActionAttibute(int methodID) { this.MethodID = methodID; } public int MethodID { get; set; } } public class ActionProcessor { public void Process(int yourInt) { var method = this.GetType().GetMethods() .Where(x => x.GetCustomAttribute(typeof(AutoActionAttribute), false) != null && x.GetCustomAttribute(typeof(AutoActionAttribute), false).MethodID == yourInt) .FirstOrDefault(); if (method != null) { method.Invoke(this, null); } } [AutoAction(1)] public DoThis() { } [AutoAction(2)] public DoThat() { } }
Update 2 (The coding I'm thinking of, says Josh K.)
// Handles all incoming requests. public class GenericProcessor { public delegate void ActionEventHandler(object sender, ActionEventArgs e); public event ActionEventHandler ActionEvent; public ProcessAction(int actionValue) { if (this.ActionEvent != null) { this.ActionEvent(this, new ActionEventArgs(actionValue)); } } } // Definition of values for request // Extend as needed public class ActionEventArgs : EventArgs { public ActionEventArgs(int actionValue) { this.ActionValue = actionValue; } public virtual int ActionValue { get; private set; } }
This creates SomeActionProcessor, which is responsible for some value:
Then create classes and connect them:
GenericProcessor gp = new GenericProcessor(); SomeActionProcessor sap = new SomeActionProcessor(); gp.ActionEvent += sap.HandleActionEvent;
Burns and sends general processor requests:
gp.ProcessAction(1);