我在c#
中写了一些代码;
if(Condition)
{
private void ribbonPanel1_Click(object sender, EventArgs e)
{
Do something ;
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
Do Something ;
}
private void ribbonPanel1_Click(object sender, EventArgs e)
{
Do something ;
}
}
这是允许的吗?我们可以在任何条件下限制多种控制方法吗?我们不仅可以使用任何循环吗?
答案 0 :(得分:1)
改为执行此操作
private void ribbonPanel1_Click(object sender, EventArgs e)
{
if (condition)
{
Do something ;
}
}
答案 1 :(得分:0)
您无法限制特定方法。但你可以做的是在特定条件下调用该方法。
public bool Condition;
private void ribbonPanel1_Click(object sender, EventArgs e)
{
if (Condition) ThingsYouWantToDo();
}
private void ThingsYouWantToD()
{
//Stuff
}
答案 2 :(得分:0)
private void ribbonPanel1_Click(object sender, EventArgs e)
{
if (Condition)
DoFirstStuff();
else if (Condition2)
DoSecondStuff();
else
DoThirdStuff();
}
但是等等......你可能在谈论条件编译器指令吗?像:
private static void Main()
{
#if DEBUG
Console.WriteLine("You are running debug version!");
#endif
Console.WriteLine("Program is starting!");
}