c# - How best to represent 'variant' enums? -



c# - How best to represent 'variant' enums? -

unfortunately i'm having interface firmware seemingly in constant state of flux (the usb communications protocol keeps changing, register/address map keeps changing, various state machines maintain changing, etc.).

i've managed implement reasonable interface, communicating firmware isn't difficult.

my main problem representing differing state machines, of there dozen. before changed had enumeration, such as:

public enum component1statetype { reset = 0, initialising = 1, ready = 2, waiting = 3 }

which used freely in main code modify software's behaviour depending on scheme state.

now have problem whereby newer firmware's component1statetype has changed this:

public enum component1statetype { reset = 0, initialising = 1, initialised = 2, ready = 3, error = 4, standby = 5, waiting = 6 }

which break previous state-handling code in main program, must of course of study back upwards different versions of firmware.

i'm struggling come way of representing these differing state machines in way means main code won't littered things like:

if (statemachineversion == 1) { //code branch version 1 } else if(statemachineversion == 2) { //code branch version 2 } ... ...

any ideas on how best deal these ever-changing states?

make interface state machine, , expose enum @ top level without providing numeric values programme outside state machine class:

interface hardwareconnector { component1statetype currentstate {get;} ... // other properties , methods } public enum component1statetype { reset , initializing , initialized , ready , error , standby , waiting }

make different implementations of hardwareconnector interface depending on version of hardware need support. code should programming hardwareconnector interface. place code should "know" class-per-hardware-version implementations initialization, observe hardware on other end.

inside each implementation class can have private enum disconnected component1statetype enumeration visible @ interface level.

internal enum component1statetypev1 { reset = 0, initialising = 1, ready = 2, waiting = 3 } internal enum component1statetypev2 { reset = 0, initialising = 1, initialised = 2, ready = 3, error = 4, standby = 5, waiting = 6 }

internals of class utilize private enum values doing work, , translate private value public value in implementation of state getter:

public component1statetypev { { switch (internalstate) { case component1statetypev1.reset : homecoming component1statetypev.reset; case component1statetypev1.initialising : homecoming component1statetypev.initializing; case component1statetypev1.ready : homecoming component1statetypev.ready; case component1statetypev1.waiting : homecoming component1statetypev.waiting; } throw new invalidoperationexception(); } }

c#

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -