Question ERROR MESSAGE IN SIMPLE CODE LINE

chazrab

Member
Joined
Nov 11, 2022
Messages
23
Programming Experience
10+
What could be wrong with this code line ? I keep getting this same error message in the images below
 

Attachments

  • ERROR MSG.jpg
    ERROR MSG.jpg
    103.3 KB · Views: 9
  • WHATS WRONG WITH THIS SIMPLE LINE OF CODE.jpg
    WHATS WRONG WITH THIS SIMPLE LINE OF CODE.jpg
    48.6 KB · Views: 9
We need to see the code you've written that throws these, really..

The } expected are simply that you missed a }, for example:


C#:
public class  X
{
    string Y;

    void MyMethod()
    {
        int i = 1;

    void MyMethod2()
    {
        int j = 1;

Not a closing } in sight. C# files need the same number of { as they do }. If you choose "Format Document" on the edit>>advanced menu and look at the indentation, you'll see where you went wrong. The code sample I made above, when reindented to add 4 more spaces upon every { will look like:


C#:
public class  X
{
    string Y;

    void MyMethod()
    {
        int i = 1;

        void MyMethod2()
        {
            int j = 1;

The last line of the file should have code with an indent level of 0, not 12..

Correct:


C#:
public class  X
{
    string Y;

    void MyMethod()
    {
        int i = 1;
    }

    void MyMethod2()
    {
        int j = 1;
    }
}
 
The listbox doesn't contain a defintion for Add is also as it says; you;ve got code like this maybe:


C#:
listbox1.Add("blah");

a ListBox doesn't have an Add method. Perhaps you meant to call listboxWhatever.Items.Add to add an item to the Items collection of the listbox

How do I know this? Familiarity with the language, mainly.. But the documentation is a great help:

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox?view=windowsdesktop-7.0

1668626916916.png



The docs frequently have useful examples and a clear organization section in the left menu of the fields, properties and methods that a class in C# has
 
Side note, I've assumed winforms with that doc link, but if you're using WPF make sure you look at the correct docs (i.e. the WPF ones, not that one)
 
Back
Top Bottom