简体   繁体   English

IF 语句中需要分号

[英]Semi-colon expected on IF statements

I'm very new to c#, this is for a currently very basic windows form to find some Autocad Variables.我对 c# 很陌生,这是一个当前非常基本的窗口表单,用于查找一些 Autocad 变量。 I'm simply trying to figure out why it's giving me ;expected after all of my "if" statements.我只是想弄清楚为什么它会给我;在我所有的“if”语句之后预期。 It has to be something very simple...or I'm approaching it wrong.它必须是非常简单的事情……否则我就错了。

note, I've tried with and without the semi-colon at the end of each and I still get the error.请注意,我已经尝试在每个末尾使用和不使用分号,但仍然出现错误。

    private void doneButton_Click(object sender, EventArgs e)
    {

        int findValue = 0;

        //int none = 0;
        //int clearAll = 1024;

        int endpoint = 1;
        int midpoint = 2;
        int center = 4;
        int node = 8;
        int quadrant = 16;
        int intersection = 32;
        int insertion = 64;
        int perpendicular = 128;
        int tangent = 256;
        int nearest = 512;
        int apparentIntersection = 2048;
        int extension = 4096;
        int parallel = 8192;

        if (cbxEndpoint.Checked) {findValue += endpoint};
        if (cbxMidpoint.Checked){findValue += midpoint};
        if (cbxCenter.Checked){findValue += center};
        if (cbxNode.Checked){findValue += node};
        if (cbxQuadrant.Checked){findValue += quadrant};
        if (cbxIntersection.Checked){findValue += intersection};
        if (cbxInsertion.Checked){findValue += insertion};
        if (cbxPerpendicular.Checked){findValue += perpendicular};
        if (cbxTangent.Checked){findValue += tangent};
        if (cbxNearest.Checked){findValue += nearest};
        if (cbxApparent.Checked){findValue += apparentIntersection};
        if (cbxExtension.Checked){findValue += extension};
        if (cbxParallel.Checked){findValue += parallel};
        if (cbxNone.Checked){findValue = 0};


     System.IO.StreamWriter file = new System.IO.StreamWriter(@"N:\C3D Support\MySettings.txt");
     file.WriteLine("OSNAPS," + findValue);
     file.Close();


      Environment.Exit(0);


      }

You should use it like this你应该像这样使用它

if (cbxEndpoint.Checked) {findValue += endpoint;}

Semi-colon inside the brackets after each line of code.每行代码后括号内的分号。 The if block does not need a semi-colon to end. if 块不需要分号来结束。

As an alternative, since your if's are one line they could be written without the brackets:作为替代方案,由于您的 if 是一行,因此可以在没有括号的情况下编写:

if (cbxEndpoint.Checked) findValue += endpoint;

The brackets are only needed if there is more than one line of code to execute when the if is true.只有当 if 为真时有不止一行代码要执行时才需要括号。 I prefer this format when writing my code:我在编写代码时更喜欢这种格式:

if (cbxEndpoint.Checked) 
{
   findValue += endpoint;
}

or或者

if (cbxEndpoint.Checked)
    findValue += endpoint;

The statements in between the curly-braces { } need to end with a semicolon.花括号 { } 之间的语句需要以分号结尾。 Not the 'if' statement itself.不是“if”语句本身。

Your code will be much more readable, and you may be able to find these mistakes more easily if you place each statement on a new line, like so:您的代码将更具可读性,如果您将每个语句放在新行中,您可能更容易发现这些错误,如下所示:

if (cbxEndpoint.Checked)
{
    findValue += endpoint
}

Now the problem is obvious.现在问题很明显了。

如果错误显示在“if”关键字下,请确保检查上面的语句是否以分号结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM