Array-Method debugging issue
I'm populating an array using a method however I receive 2 errors of a same issue when trying to assign the hole array using the method and when returning the populated array in the method!
I've added explicit comments with the errors on the problematic lines
static void Main(string[] args)
{
uint n = uint开发者_如何学C.Parse(Console.ReadLine());
uint m = uint.Parse(Console.ReadLine());
int[,] array=new int [n,m];
array=FillArrayMethodC(n,m); //Cannot implicitly convert type 'int' to 'int[*,*]'
}
static int FillArrayMethodC(uint n, uint m)
{
int[,] a=new int [n,m];
//fill the array
return a; //Cannot implicitly convert type 'int[*,*]' to 'int'
}
How is the proper way of returning the whole array and assigning it!!!
Thank you for your help BR
Change the return type of the FillArrayMethod.
static int[,] FillArrayMethodC(uint n, uint m)
It's unclear whether you want to create a new array in your Main or in your fill method. It looks like you might actually want this:
static void Main(string[] args)
{
uint n = uint.Parse(Console.ReadLine());
uint m = uint.Parse(Console.ReadLine());
int[,] array=new int [n,m];
FillArrayMethodC(array, n, m); // pass array into "fill" method
}
static void FillArrayMethodC(int[,] a, uint n, uint m)
{
//fill the array
...
// nothing to return
}
What's happening here is that your Main function is creating the array and passing it into the fill method. The fill method will not create the array so it will not return anything.
There's no need to create the array in your FillArrayMethodC() method, since you've already created it. If that method is just filling the array and needs to know the array dimensions, you can just pass the array into the method and get the dimensions inside of that method. I suspect that you want something like this:
static void Main(string[] args)
{
uint n = uint.Parse(Console.ReadLine());
uint m = uint.Parse(Console.ReadLine());
int[,] array = new int[n, m];
FillArrayMethodC(array);
}
static void FillArrayMethodC(int[,] arr)
{
int numRows = arr.GetLength(0);
int numCols = arr.GetLength(1);
for (int i = 0; i < numRows; i++)
for (int j = 0; j < numCols; j++)
{
// Fill your array here, e.g.:
arr[i, j] = i * j;
}
return;
}
精彩评论