Home > Software engineering >  [ C# ][ Errors: CS0443, CS0120, CS1503 ] Can't pass a bidimensional array to a function
[ C# ][ Errors: CS0443, CS0120, CS1503 ] Can't pass a bidimensional array to a function

Time:10-29

Hello I'm trying to pass an array to a function. First I was trying to pass it by value but apparently it has to be done by reference so I added a "ref". This gives me a "CS0443: A value was expected":

Render(ref cadena[,]);

public static void Render(ref char[, ] cadena)

So I give it a value. I found an example that said you should point it to the index, so I put 0,0:

Render(ref cadena[0,0]);

public static void Render(ref char[, ] cadena)

This gives me the CS1503 error ("can't convert from 'ref char' to 'ref char[,]'). I don't know what this means. Doesn't the function expect a char[,], and am I not giving it a char[,] type (cadena)?

Here is the full code:

using System;

namespace B
{
    class Program
    {
        static void Main(string[] args)
        {
            char[,] cadena = new char[30,30];
            for (int j = 0; j < 30; j  )
            {
                for (int i = 0; i < 30; i  )
                {
                    cadena[i, j] = ' ';
                }
            }

            while (0 == 0)
            {
                int posX, posY;
                char modo = 'E';
                Console.Write("¿Escritura o borrado?");
                modo = char.Parse(Console.ReadLine());
                Console.Write("¿Posición X?");
                posX = int.Parse(Console.ReadLine());
                Console.Write("¿Posición Y?");
                posY = int.Parse(Console.ReadLine());
                //Problema: limitar las posiciones a 0-29

                if (modo == 'E')
                {
                    cadena[posX, posY] = 'X';
                }
                if (modo == 'B')
                {
                    cadena[posX, posY] = ' ';
                }
                else if ((modo != 'E') || (modo != 'B'))
                {
                    Console.WriteLine("Error.");
                }
                Render(ref cadena[0,0]);
                
            }
            
        }

        void Draw()
        {
            //point A (x1, y1)
            //point B (x2, y2)
            return;
        }

        public static void Render(ref char[, ] cadena)
        {
            Console.WriteLine("    0 1 2 3 4 5 6 7 8 9 - - - - - - - - - - 0 1 2 3 4 5 6 7 8 9");
            Console.WriteLine("    - - - - - - - - - - 0 1 2 3 4 5 6 7 8 9 - - - - - - - - - -");
            Console.WriteLine("    -----------------------------------------------------------");
            for (int j = 0; j < 30; j  )
            {
                if (j < 10)
                {
                    Console.Write(j   "  | ");
                }
                else
                {
                    Console.Write(j   " | ");
                }
                for (int i = 0; i < 30; i  )
                {
                    Console.Write(cadena[i, j]   " ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
            return;
        }
    }
}

Thank you

CodePudding user response:

The method is expecting the entire array:

public static void Render(ref char[, ] cadena)

You're passing it only one value from that array:

Render(ref cadena[0,0]);

Either pass it the entire array:

Render(ref cadena);

Or change the method to only expect (and use) one char value, not an entire array.


As an aside, there is absolutely no reason to be passing by reference here. The use cases for passing by reference are pretty few and far between. Remove the ref keywords and just pass the value you want.

From the description it sounds like a series of unsolved problems led you down this rabbit hole. You may need to back-track quite a bit to simplify your implementation and "fix" your program. In general, when you encounter an error, don't just search for the error and copy/paste the first thing you find. In your case it sounds like this was just (unsurprisingly) producing another error. If you don't understand an error and don't know what it's directing you to change and why, that is a good candidate for a Stack Overflow question.

  • Related