Invalid token 'while' in class, struct, or interface member declaration in very simple code
Asked Answered
C

6

10

I am not sure what the problem is but I keep receiving this error when I try to use a while statement in my code.

Invalid token 'while' in class, struct, or interface member declaration

I want to use a while loop to have something continuously update while a statement is true.

The rest of my code is rather long but whenever I type in the syntax:

while(a<b)
{
//do whatever i want it to do here
}

It gives me that compiler error right off the bat. Not quite sure what the problem is. I am doing this in a C# windows application under the Form1.cs file with all the other event handlers (for buttons and such). Thanks!


I was unaware that loops had to be placed within a method (fairly new to c#), but I tried it and no errors were returned. Thanks for your help everybody!

Previously, I just had the loop within the main class of the program.

Camara answered 29/12, 2008 at 2:38 Comment(0)
D
39

Based on the error, it sounds like the compiler thinks this code is typed directly in the body of a class/struct/interface declaration. Statements while/if/for/etc ... must appear with in a method.

Try moving this code into a method to fix the problem. If it's in a method, you likely have a mismatched brace problem.

Daw answered 29/12, 2008 at 2:40 Comment(0)
C
4

There's nothing wrong with the while, it's something above it that's the problem. Check for mismatched braces and semicolons in a comment or something like that.

Circassian answered 29/12, 2008 at 2:39 Comment(0)
W
2

C# is not allowed to write code directly into the classes; it is allowed to write only data members and function members directly into the classes.

Wording answered 22/6, 2011 at 8:47 Comment(0)
V
0

You can also get this if you have punctuation issues, I got it today when I missing a simple parentheses:

public static string GetPasswordForEnvironment)

should have been:

public static string GetPasswordForEnvironment()

But the error showed up on the first "if" statement later in the function.

Victim answered 5/2, 2015 at 14:52 Comment(0)
B
0

C# does not provide writing the for, while or foreach kind of looping statement directly inside the class. These statements should be within a user defined method, main method or constructors.

  class Array
{
    public static void main(string[] args)
    {
        int[] n = new int[10]; /* n is an array of 10 integers */

        /* initialize elements of array n */
        for (int i = 0; i < 10; i++)
        {
            n[i] = i + 100;
        }

        /* output each array element's value */
        foreach (int j in n)
        {
            int i = j - 100;
            Console.WriteLine("Element[{0}] = {1}", i, j);
        }
    }


}
Bloater answered 12/3, 2019 at 18:40 Comment(0)
F
0

I had this issue today and like someone said previously the issue was above where the error was flagged.

In my case, it was an misplaced curly bracket.

    } // This bracket needed to be deleted


      switch (example) { 
Functionary answered 17/4, 2024 at 12:47 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Ellette

© 2022 - 2025 — McMap. All rights reserved.