Program Flow using Events
I'm trying to write a program where Step 1 -> Kick off Step 2 -> kick off
step 3. Variables are passed in between each step.
Can I use events this way in C#? What might be the best way to write a
program that does this?
public class ProgramFlow // the listener program
{
EventArgs args = null;
public delegate void EventHandler(string str, EventArgs e);
public static event EventHandler Step1Reached;
public static event EventHandler Step2Reached;
public ProgramFlow()
{
Step1 step1 = new Step1();
// Print string and kick off Step2
Step2 step2 =new Step2();
// Print String kick off next step
}
}
public class Step1
{
string charRead;
public Step1()
{
Console.Write("Input something for Step1: ");
charRead = Console.ReadLine();
Console.WriteLine();
ProgramFlow.Step1Reached += ProgramFlow_Step1Reached;
}
void ProgramFlow_Step2Reached(string str, EventArgs e)
{
Console.WriteLine(charRead);
}
}
public class Step2
{
string charRead;
public Step2()
{
Console.Write("Input something for Step2: ");
charRead = Console.ReadLine();
Console.WriteLine();
ProgramFlow.Step2Reached += ProgramFlow_Step2Reached;
}
void ProgramFlow_Step2Reached(string str, EventArgs e)
{
Console.WriteLine(charRead);
}
}
class Program
{
static void Main(string[] args)
{
ProgramFlow programFlow = new ProgramFlow();
Console.ReadKey();
}
}
No comments:
Post a Comment