Hi,
It's not really a state machine if you're just "flagging" state. In other
words, if you're not performing any operations based on the current state then
enums should be just fine (as mentioned by another respondent). Although you
might want to use multiple enums instead of a single integer.
enum BeltDirection { Forward, Backward }
enum BinPosition { Start, Middle, End }
enum BinEnabledState { On, Off } // bool would be much easier ;)
If you want to use a flexible state machine architecture instead that actually
does provide different runtime logic depending on the current "state" of the
transport belt you could code something like this:
class TransportBeltCo ntroller
{
private TransportBeltSt ate state;
public TransportBeltSt ate State
{
get
{
if (state == null)
state = new TransportBeltFo rwardState();
return state;
}
set
{
if (value == null)
throw new ArgumentNullExc eption("State") ;
state = value;
}
}
public BinPosition MoveToNextBin()
{
State.MoveToNex tBin();
return state.Position;
}
}
enum BinPosition { Start, Middle, End }
abstract class TransportBeltSt ate
{
protected BinPosition binPos; // Start is default
public BinPosition Position { get { return binPos; } }
public abstract void MoveToNextBin() ;
}
class TransportBeltFo rwardState : TransportBeltSt ate
{
public override void MoveToNextBin()
{
if (binPos == BinPosition.End )
binPos = BinPosition.Sta rt;
else
binPos = ((int) binPos)++;
}
}
class TransportBeltBa ckwardState : TransportBeltSt ate
{
public override void MoveToNextBin()
{
if (binPos == BinPosition.Sta rt)
binPos = BinPosition.End ;
else
binPos = ((int) binPos)--;
}
}
--
Dave Sexton
"nyhetsgrup per" <ny***********@ gmail.comwrote in message
news:es******** *****@TK2MSFTNG P02.phx.gbl...
>I have a C++ application which I am porting to C#. This application is
controlling a transport belt. This transport belt is moving bins foreward and
backward. I have a state machine controlling this transport belt. In c++
there is an integer named state which holds the state of the belt. Bit 0-2 is
telling which task is going on (move belt forward, move belt to middle
possition and so on..) Bit 3 is telling if there is a bin in the middle
possition of the belt. Bit 4-7 is telling some thing about the physical state
of the belt. Bit 8-11 is telling something about exceptions. This is working
well, but I think it is a little hard to understand if someone is going to
take over my code someday. I want to make this simpler. Maybe using enums and
the flags attribute? What do you think?
Thank you.