2013-04-27 58 views
-4

我正在Visual Studio中创建一个可执行文件。如何让我的程序(exe)停止(而不是退出)一旦发生事件(生成文本文件)?

我的代码是这样的:

if(condition) 
    goto Step 1 
else 
    goto Step 2 


Step 1: 
code 


Step 2: 
code 

我想使这个的方式,如果第1步已经运行,那么第2步必须被跳过。

应该使用函数来完成吗?

+1

,除非你有一个很好的理由,请不要使用goto语句。在这种情况下,函数几乎可以肯定更好 – Joel 2013-04-27 20:14:31

回答

1

在你的班级里它可以放在两个函数中,并从if-else逻辑中调用,或者你可以把代码放在if和else之间。如果代码很大,那么创建两个函数会更好。

If (condition) 
    call function step1 
else 
    call function step2 

or 

if (condition) 
    code... 
else 
    code... 

C# Example of defining a method and calling it:

public void Caller() 
{ 
    int numA = 4; 
    // Call with an int variable. 
    int productA = Square(numA); 

    int numB = 32; 
    // Call with another int variable. 
    int productB = Square(numB); 

    // Call with an integer literal. 
    int productC = Square(12); 

    // Call with an expression that evaulates to int. 
    productC = Square(productA * 3); 
} 

int Square(int i) 
{ 
    // Store input argument in a local variable. 
    int input = i; 
    return input * input; 
} 
+0

我用你的第二个建议做到了这一点,但无法使用函数:(在C#中使用函数比在C中使用函数更复杂 – ABX 2013-04-27 20:21:02

+1

@ABX发布你的实际代码在C#中使用函数并不比C复杂得多。两者都非常简单(以及语言的基本方面)。 – Kitsune 2013-04-27 20:24:00

+0

已更新的答案,以及如何在C#中创建和调用函数的示例 – SoftwareCarpenter 2013-04-27 20:34:06

相关问题