C# violates its own syntax

K6M

New member
Joined
Dec 29, 2022
Messages
1
Programming Experience
1-3
I am currently working on a snake game project for my polytechnic graded project. However, C# seems to be violating its own syntax in this particular WinForms component (look at the highlighted text). Anyone know how I might be able to fix this?
 

Attachments

  • Screenshot 2022-12-29 235919.png
    Screenshot 2022-12-29 235919.png
    277.5 KB · Views: 24
What do you think should be happening that isn't or shouldn't be happening that is? Try providing an actual explanation of the problem.

If you think that you should be able to write a line of code at the class level that is not a declaration then it's your expectations that are at fault. Line 19 declares the scores field. Line 20 declares the dummy field. Line 21 doesn't declare anything. If you want an item field, a collection field and the item in the collection then use a collection initialiser, e.g.
C#:
private Score dummy = new Score("dummy", 0, 0);
private List<Score> scores = new List<Score> { dummy };
Note that I have also added explicit access modifiers to those fields. It is good practice to ALWAYS explicitly specify the access level of ALL members.

By the way, please don't post code as only screenshots in future. Provide an additions screenshot if it adds value but ALWAYS post your code as text, formatted as code. I had to retype your code from scratch because I couldn't copy it from your screenshot.
 
I had to retype your code from scratch because I couldn't copy it from your screenshot.
One of the things I've really come to appreciate about the iPhone is it'll ocr images automatically.. never thought I'd prefer posting in code forums from a phone but on occasion.. :)
 

Latest posts

Back
Top Bottom