Search code examples
c#classconstructorcurly-braces

Why does my code not realise there is a } to close the constructor off


I have been using Visual Studio Code for my C# code; however, it does not seem to realise that I have got a curly bracket '}' at the end of the constructor for my 'opponent' class. Please help me find a way to make it realise that I have done it correctly as far as I can tell!

Here is the class:

public class opponent
{
    public int hp = 20;
    public int PDmg = 3;
    public int KDmg = 3;

    public void Opponent(int lv = 1)
    { # red line error marker here
        public int level = lv;
    }
}

And here is the error message:

} expected [miscellaneousFiles.csproj]

Please help!

I have tried changing the name and capitalisation of the method name but it has not worked so the case-sensitivity of C# is not the problem. I have also tried removing the word 'void' and it did not work either.


Solution

  • double click on the error and it will take you to the place you have missed the brace maybe its not this code where you have got the error

    From this line

    public int level = lv;
    

    remove public and its gonna work