# C# using structs

## Recommended Posts

da_cobra    122
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?!?

##### Share on other sites
Guest Anonymous Poster
You can't declare a struct within a method.

##### Share on other sites
Gage64    1235
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#.

##### Share on other sites
da_cobra    122
that's it indeed, thx!