smakfactor1
smakfactor1

Reputation: 71

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. I'm simply trying to figure out why it's giving me ;expected after all of my "if" statements. 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);


      }

Upvotes: 3

Views: 3034

Answers (3)

IvoL
IvoL

Reputation: 109

If the error shows under the 'if' keyword then make sure to check that the statement above ends with a semicolon.

Upvotes: 1

Breealzibub
Breealzibub

Reputation: 8095

The statements in between the curly-braces { } need to end with a semicolon. Not the 'if' statement itself.

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.

Upvotes: 1

RacerNerd
RacerNerd

Reputation: 1577

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.

As an alternative, since your if's are one line they could be written without the brackets:

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. I prefer this format when writing my code:

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

or

if (cbxEndpoint.Checked)
    findValue += endpoint;

Upvotes: 3

Related Questions