02-02-2013 03:23 AM
what is the advantages/disadvantages of an enum vs strings for state machines
02-02-2013 05:15 AM
02-02-2013 12:08 PM
I agree with Mike that enums have a great advantage in their strong typing. You can't make spelling mistakes and if you set up the case structure not to have a default value, you can usually be able to guarantee that you will handle every new value because the code will break when you add it.
That said, I don't agree that strings don't have advantages. The main advantage I can think of is if you want to have standard reuse code for handling your state machine (such as error state injection or logging). This isn't possible with enums because each enum will be considered a different data type.
Ultimately, I think it comes down to a matter of personal preference. If your state machines are simple and flat, an enum will probably work better. If you have some complexity or you want to have many SMs with reuse VIs, strings might be preferable.
02-02-2013 12:54 PM
At times I hate replying to these esoteric questions but,.....
A type defed enum restricts acess to the SM to the set of callers that have acess to the enum definition. That definition us usually contained in the project of the SM and therefore the caller must have some knowledge of the SM (Its capabilities) This "overcouples" the callers to the callee but, is usually not a barrier to executing the LabVIEW developers intention since the caller breaks when the type def changes are not applied to the caller. When applied to the caller they change the caller without high visability (may cause unexpected behavior if cases swap order)
Strings, on the other hand, are universal so, changes in the SM being called do not break the caller they mearly cause run-time errors if the case name strings change that require the caller to be modified to call the callee with new correct instructions.
Like I said, pretty esoteric. With either method the caller needs to be updated if changes are made to the SM. One breaks at edit or changes unexpectedly, one errors at runtime. As tst said it comes down to a matter of preference.
02-02-2013 03:54 PM
And you're welcome to read this or search for more - http://forums.ni.com/t5/LabVIEW/Why-is-the-Continuous-Measurement-and-Logging-Sample-Project-in/m-p/...
02-03-2013 08:46 AM
Have you tried a object oriented state machine?
https://decibel.ni.com/content/docs/DOC-24499
Br,
/Roger