I've just started using C# and this piece of code is throwing up all sorts of errors: Code: void resetValids(List<int> x) { x.Clear(); x.Add(0); x.Add(1); x.Add(2); x.Add(3); x.Add(4); x.Add(5); x.Add(6); x.Add(7); x.Add(8); } List<int> validMoves = new List<int>(); resetValids(validMoves); I get these errors: Identifier expected 'WindowsFormsApplication1.Form1.validMoves' is a 'field' but is used like a 'type' Method must have a return type Any clues?
Code: #include <list> void resetValids(std::list<int> x) { x.clear(); x.push_front(0); x.push_front(1); x.push_front(2); x.push_front(3); x.push_front(4); x.push_front(5); x.push_front(6); x.push_front(7); x.push_front(8); } int _tmain() { std::list<int> validMoves = std::list<int>(); resetValids(validMoves); return 0; } This works in C++, haven't actually tested in C#.
The code's pretty much the same for C#. For the OP, you're getting that error because you're trying to call a function outside of an existing function, without it actually being used to set a value for a declared variable. This will work, for example: Code: List<int> x = resetValids(); List<int> resetValids() { List<int> x = new List<int>(); x.AddRange( Enumerable.Range (0,9 ) ); return x; } That will work because it's setting the value of a declared variable, whereas you're is taking an already set variable, and modifying it. Moving the resetValidss function call into the form constructor will prevent the first and third errors. I think it should also fix the second error, but I'm unsure because I've not gotten it this end.
I'd have to see all your code in context to understand the error message properly, but the code itself is fine (see below) However, it's not doing what I think you want it to do. I think you want your resetValids method to reset your validMoves variable to contain the integers from 0-8 but at the moment all the resetValids method does is create a variable (x) that immediately goes out of scope as soon as the method is finished. What you need to do is pass your original variable by reference e.g. Code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } void resetValids(ref List<int> x) { x.Clear(); x.Add(0); x.Add(1); x.Add(2); x.Add(3); x.Add(4); x.Add(5); x.Add(6); x.Add(7); x.Add(8); } private void Form1_Load(object sender, EventArgs e) { List<int> validMoves = new List<int>(); this.resetValids(ref validMoves); } } } Passing by reference is ugly. The way I would have done it is: Code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } List<int> getValidMovesList() { List<int> x = new List<int>(); x.Clear(); x.Add(0); x.Add(1); x.Add(2); x.Add(3); x.Add(4); x.Add(5); x.Add(6); x.Add(7); x.Add(8); return x; } private void Form1_Load(object sender, EventArgs e) { List<int> validMoves = this.getValidMovesList(); } } } Here is the original listing I tried and it works fine: Code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } void resetValids(List<int> x) { x.Clear(); x.Add(0); x.Add(1); x.Add(2); x.Add(3); x.Add(4); x.Add(5); x.Add(6); x.Add(7); x.Add(8); } private void Form1_Load(object sender, EventArgs e) { List<int> validMoves = new List<int>(); this.resetValids(validMoves); } } }