> Hi,
I have a windows form project. The form class has a number of methods
in it that i want to call from another class I have just created.
This class is a finite state machine and is created and kicked off in
same method where the applications' Run method is called. i.e main
method in Program.cs.
I'd appreciate suggesstions for calling methods in the form class
from this finite state maching class.
Regards
Macca
I'd implement this either using events or interfaces. Either of those two
solutions would allow you to implement the code you need and make the state
machine call into your form, without the state machine being tied to that
particular form. Examples of both follow.
Example 1, events:
public class StateMachine
{
public event EventHandler Completed;
public void OnCompleted()
{
if (Completed != null)
Completed(this, EventArgs.Empty );
}
public void Execute()
{
OnCompleted();
}
}
public class MainForm
{
public void DoSomething()
{
StateMachine sm = new StateMachine();
sm.Completed += new EventHandler(St ateMachineCompl eted);
sm.Execute();
}
public void StateMachineCom pleted(Object sender, EventArgs e)
{
...
}
}
Example 2, interfaces:
public interface IStateMachineEv ents
{
void Completed();
}
public class StateMachine
{
private IStateMachineEv ents _Events;
public StateMachine(IS tateMachineEven ts events)
{
_Events = events;
}
public void OnCompleted()
{
if (_Events != null)
_Events.Complet ed();
}
public void Execute()
{
OnCompleted();
}
}
public class MainForm : IStateMachineEv ents
{
public void DoSomething()
{
StateMachine sm = new StateMachine(th is);
sm.Execute();
}
void IStateMachineEv ents.Completed( )
{
...
}
--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vk arlsen.no
PGP KeyID: 0x2A42A1C2