C# using structs

Started by
2 comments, last by da_cobra 17 years, 1 month ago
Hello, I'm rather new to the C# language, but I know C++. Now I came upon a problem when using a struct and I can't seem to figure it out for the moment : I want to create a highscore table (I just started writing the code, so I do not have much for now).

public int NewHighscoreTable(string strDefaultName, long lDefaultScore, long lDefaultLowerScore)
        {
            FileStream fs = File.Create("highscore.dat"); 

            struct highscoreLine
            {
                public string strName;
                public int iScore;
            }

            highscoreLine hl = new highscoreLine(); 

            return 0;
        }

When I execute this code I get the following errors : Error 1 } expected => points to the semicolon (;) right after the FileStream line Error 2 Invalid token 'return' in class, struct, or interface member declaration Error 3 Type or namespace definition, or end-of-file expected When I comment out the whole struct lines, then the errors are gone and I can execute my code with no problem. Am I missing something here?!?
Advertisement
You can't declare a struct within a method.
Maybe try to declare the struct outside of the method?
I'm not sure if you can declare a struct/class inside a method (that is, a local struct/class). You can in C++, but I don't know about C#.
that's it indeed, thx!

This topic is closed to new replies.

Advertisement