Muffinator
Muffinator

Reputation: 143

Function not returning array

I have this function to initiate a two dimensional array:

static Array Matrix(int Rows, int Columns) 
{
    int[,] LotteryArray = new int[Rows,Columns];

    for (int i = 0; i < LotteryArray.GetLength(0); i++) 
    {
        for (int j = 0; j < LotteryArray.GetLength(1); j++)
        {
            LotteryArray[i, j] = RandomNum(1, 46);  
            Console.Write("{0,3},", LotteryArray[i, j]); 
        }
        Console.WriteLine();
    }
    return LotteryArray;
}

Then I have this which is supposed to give me a one dimensional array and see how many numbers in the winning array are in the matrix:

int RowNum = 1;
int Prediction = 0;
Console.WriteLine("Your winning numbers are!");
Console.WriteLine("------------------------");
int[] Winner = new int[6];
for (int i = 0; i < Winner.Length; i++)  //this loop is to initiate and print the winning numbers
{
    Winner[i] = RandomNum(1, 46);        //the numbers are supposed to be between 1 and 45, so i tell it to do it until 46 because the upper limit is exclusive
    Console.Write("{0,3},", Winner[i]);
}

Console.WriteLine();
Console.WriteLine("------------------------"); //these two lines are for aesthetics 
Matrix(Rows, Columns);


foreach (int i in Winner) 
{
    for (int j = 0; j<LotteryArray; j++)
    {
        if (Winner[i] == j)
        {
            Prediction++;
            if (j % 6 == 0) { RowNum++; }
        }
        Console.WriteLine("you got {0} correct prediction in row number {1}",Prediction,RowNum);
        RowNum = 1;
   }
}

It's telling me LotteryArray doesn't exist in the current context.

Upvotes: 0

Views: 61

Answers (2)

Melquiades
Melquiades

Reputation: 8598

LotteryArray is a variable declared in Matrix method, and is not visible outside.

Upvotes: 0

Praveen Paulose
Praveen Paulose

Reputation: 5771

LotteryArray is a variable within another method. You cannot access it in the scope you are showing.

You can do get the return from your method into a variable and then use it.

var LotteryArray = Matrix(Rows, Columns);

foreach (int i in Winner) 
    {
        for (int j = 0; j<LotteryArray; j++)
        {
            if (Winner[i] == j)
            {
                Prediction++;
                if (j % 6 == 0) { RowNum++; }
            }
            Console.WriteLine("you got {0} correct prediction in row number {1}",Prediction,RowNum);
            RowNum = 1;
        }
    }

Upvotes: 3

Related Questions