我正在尝试编写一个程序,其中步骤 1 -> 启动步骤 2 -> 启动步骤 3。变量在每个步骤之间传递。
我可以在 C# 中以这种方式使用事件吗?编写执行此操作的程序的最佳方法可能是什么?
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();
}
}