0
static Array Matrix(int Rows, int Columns)
{
    int[,] Lottery = new int[Rows,Columns];  
    for (int i = 0; i < Lottery.GetLength(0); i++) 
    {
        for (int j = 0; j < Lottery.GetLength(1); j++)
        {
            Lottery[i, j] = RandomNum(1, 46);       
            Console.Write("{0,3},", Lottery[i, j]); 
            Console.WriteLine();
        }
        return Lottery;
    }
}

I have this function to initiate and print a matrix, and I want to do a foreach loop that checks all the numbers in each row, but when I do foreach (int i in Matrix), it tells me that I cannot operate on a method group, and when I do foreach (int i in Lottery), it tells me that Lottery is a namespace.

I'm a beginner and I don't know what to do.

3
  • foreach (int i in Matrix) is incorrect because you should invoke Matrix method with 2 parameters; try foreach (int i in Matrix(3,4)) Commented Apr 18, 2015 at 15:58
  • Additionally you should change your method signature to return an int[] like this: private static int[,] Matrix(int Rows, int Columns) Commented Apr 18, 2015 at 16:08
  • 1
    note also that you can declare your method as static int[,] Matrix(int Rows, int Columns); it will be type-safe and will allow to use the same nested for loops with result of method Commented Apr 18, 2015 at 16:09

1 Answer 1

2

There is no meaning in calling like

foreach (int i in Matrix)

because Matrix is a Funtion or Method. And also you are not passing parameters required for that function..

For your need first prepare the Array, and then check..

Do the checking like this

Array lottery = Matrix(5, 5);
foreach (int number in lottery)
{
    // check number as required
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.