C# Errors | Sololearn: Learn to code for FREE!


C# Errors

Can anyone help? I don't see what's wrong and I don't understand the errors. I think I've gotten too used to Python’s readability. Lol Code: https://code.sololearn.com/cnw4M7i8UYod/?ref=app

3/10/2018 10:25:31 PM


6 Answers

New Answer


Variables can only be declared inside a class or method. Move number1, number2, and answer to main(). By the way, those 5 lines at the top of your code are importing other namespaces you can use. Ex: "using System;" allows you to use Console.WriteLine(); instead of System.Console.WriteLine(); using System; is all you really need, but you can leave them all.


Oh and the explanation, sorry, well in Java, static members only belong to the class. If you make an object out of the class, the object has no influence on the static member. Example class Enemy{ static private int numEnemies = 0; private int x; // x axis private int y; // y axis public Enemy(){ numEnemies++; // this will increase everytime an object is instantiated, and keep its previous value x = 0; // All enemies carry these values and can change them y = 0; } } And for the first comment, the variables belong to the main scope so its ok ;)


IDK C# but moving your variables inside the static main function made it work. Being used to Java made this look unattractive so I moved it lol Sorry, hopefully someone explains it better


And moving them inside the class brackets above main, and making them static ints also worked! Like Java :D


@Tamra, @(JapaneseTextICan’tFindOnMyJapaneseKeyboard), Thanks!