i have my main and a function that deals with an array.

public static void Main(string[] args)
        {
            Pieces [,] pieces; 
            ChessBoard(out pieces[,]); // The IDE shows that the mistake is here.
            It says that the overloaded method has some invalid arguments!
            How can i make it so that pieces will obtain the array?
            
        }
public static void ChessBoard(out Pieces [,] piece )
        {
                    piece =new Pieces[9,9];

                    piece[1,1]=new Rook("WR");
                    piece[1,2]=new Knight("WKN");
                    piece[1,3]=new Bishop("WB");
                    piece[1,4]=new Queen("WQ");
                    piece[1,5]=new King("WK");
                    piece[1,6]=new Bishop("WB");
                    piece[1,7]=new Knight("WKN");
                    piece[1,8]=new Rook("WR");


                    piece[2,1]= new Pawn("WP");
                    piece[2,2]=new Pawn("WP");
                    piece[2,3]=new Pawn("WP");
                    piece[2,4]=new Pawn("WP");
                    piece[2,5]=new Pawn("WP");
                    piece[2,6]=new Pawn("WP");
                    piece[2,7]=new Pawn("WP");
                    piece[2,8]=new Pawn("WP");

                    piece[7,1]= new Rook("BR");
                    piece[7,2]= new Knight("BKN");
                    piece[7,3]= new Bishop("BB");
                    piece[7,4]= new Queen("BQ");
                    piece[7,5]= new King("WK");
                    piece[7,6]= new Bishop("BB");
                    piece[7,7]= new Knight("BKN");
                    piece[7,8]= new Rook("BR");

                    piece[8,1]=new Pawn("BP");
                    piece[8,2]=new Pawn("BP");
                    piece[8,3]=new Pawn("BP");
                    piece[8,4]=new Pawn("BP");
                    piece[8,5]=new Pawn("BP");
                    piece[8,6]=new Pawn("BP");
                    piece[8,7]=new Pawn("BP");
                    piece[8, 8] = new Pawn("BP");
          
        }

Becuase you are using static methods, you can only use the objects inisde of them, and whats is passed from one to another. This is what you can do:

class MyClass
{   
    public static void Main(string[] args)
    {        
       Pieces[,] = ChessBoard();
    }

    public static Pieces[,] ChessBoard( )
    {
        Pieces [,] pieces = new Pieces[9,9];
        //code to fill the pieces (which you have alreay)
  
        return pieces;
    }
}

This will do it.
Mitja

instead of your first line, i wrote this:

Pieces [,] pieces = ChessBoard();

and it showed no mistake.


but why i cant use the "out" modifier?
what was the problem with it?

Using of out method parameter its good too. You can use it if you want in your example, but its pointless to pass the parameter into the method, that will be returned as well. Mostly you pass parameters that you need in methods, and return those that you need in the previous method, like a simple example:

static void Main(string[] args)
        {
            int value = 2;
            List<string> list = CreateList(value);

            for (int i = 0; i < list.Count; i++)
                Console.WriteLine("{0}", list[i]);
            Console.ReadLine();
        }

        private static List<string> CreateList(int howMany)
        {
            List<string> list = new List<string>();
            for (int i = 0; i < howMany; i++)
            {
                string sItem = Calculate(i);
                list.Add(sItem);
            }
            return list;
        }

        private static string Calculate(int i)
        {
            string item = null;
            switch (i)
            {
                case 0:
                    {
                        item = "one";
                        break;
                    }
                case 1:
                    {
                        item = "two";
                        break;
                    }
                case 2:
                    {
                        item = "three";
                        break;
                    }
                default:
                    {
                        item = "none";
                        break;
                    }
            }
            return item;
        }

I hope you get the point. SUM: you don`t need to pass the parameter into the method from which will be the same returned. Actually you can, but this is not the proper way if you get me. But if you want you still can do it, and use out method parameter.
Just that!

Please take both examples, in the upper post and this one in here.
The same example would be with using keyword "out":

static void Main(string[] args)
        {
            int value = 2;
            List<string> list;
            CreateList(out list, value);

            for (int i = 0; i < list.Count; i++)
                Console.WriteLine("{0}", list[i]);
            Console.ReadLine();
        }

        private static void CreateList(out List<string> list, int howMany) 
        {
            list = new List<string>();
            for (int i = 0; i < howMany; i++)
            {
                string sItem;
                Calculate(out sItem, i);
                list.Add(sItem);
            }
        }

        private static void Calculate(out string item, int i)
        {
            switch (i)
            {
                case 0:
                    {
                        item = "one";
                        break;
                    }
                case 1:
                    {
                        item = "two";
                        break;
                    }
                case 2:
                    {
                        item = "three";
                        break;
                    }
                default:
                    {
                        item = "none";
                        break;
                    }
            }
        }
commented: Thank you +0

Alright, i got it. could you tell me lastly , should i try to keep my main as short as possible and spread all the statements across function. as i did there?

Yes, exactly. Main method as short as possible, and so the other methods as well. Its always good to use a particuar method only for one thing, and one thing only. You never know when you are gonna use it again (it happens many times, that you use a method many times during opertions).
Happy coding,
bye, bye
Mitja

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.