1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development Need C# help

Discussion in 'Software' started by TCoZ, 13 Jul 2010.

  1. TCoZ

    TCoZ Buffalo buffalo buffalo buffalo...

    Joined:
    3 May 2010
    Posts:
    277
    Likes Received:
    8
    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?
     
  2. capnPedro

    capnPedro Hacker. Maker. Engineer.

    Joined:
    11 Apr 2007
    Posts:
    4,381
    Likes Received:
    241
    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#.
     
  3. Measter

    Measter What's a Dremel?

    Joined:
    2 Feb 2008
    Posts:
    129
    Likes Received:
    4
    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.
     
    TCoZ likes this.
  4. TCoZ

    TCoZ Buffalo buffalo buffalo buffalo...

    Joined:
    3 May 2010
    Posts:
    277
    Likes Received:
    8
    Okay, thanks, that fixed it.
     
  5. Daedelus

    Daedelus What's a Dremel?

    Joined:
    7 May 2009
    Posts:
    253
    Likes Received:
    12
    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);
            }
        }
    }
     
    Last edited: 14 Jul 2010
Tags:

Share This Page