-1
class Program
{
    int[] arrayOne = new int[3];
    int[] arrayTwo = new int[5];
    int[] arrayThree = new int[7];
    int sumOne = 0;
    int sumTwo = 0;
    int sumThree = 0;

    static void Main()
    {

        //call method to generate numbers
        generateRandomNumbers();

        //call method to get sums of arrays
        sumOne = getSum(arrayOne);
        sumTwo = getSum(arrayTwo);
        sumThree = getSum(arrayThree);

        //call method to show numbers in each array and their sum
        showNumbers(arrayOne);
        showNumbers(arrayTwo);
        showNumbers(arrayThree);

        ReadKey();
    }

    private static void generateRandomNumbers()
    {
        Random random = new Random();
        //load in values for arrayOne
        for (int i = 0; i < arrayOne.Length; i++)
            arrayOne[i] = random.Next(1, 99);

        //arrayTwo
        for (int j = 0; j < arrayTwo.Length; j++)
            arrayTwo[j] = random.Next(1, 99);

        //arrayThree
        for (int k = 0; k < arrayThree.Length; k++)
            arrayThree[k] = random.Next(1, 99);
    }

    private static int getSum(int[] array)
    {
        int total = 0;

        for (int x = 0; x < array.Length; x++)
            total += array[x];

        return total;
    }

    private static void showNumbers(int[] array)
    {
        for (int m = 0; m < array.Length; m++)
            Write("{0, 5}", array[m]);

        switch (array.Length)
        {
            case 3:
                Write(" = {0}", sumOne);
                break;
            case 5:
                Write(" = {0}", sumTwo);
                break;
            case 7:
                Write(" = {0}", sumThree);
                break;
        }
        WriteLine();

    }
}

Basically, the error occurs everywhere that I use arrayOne, arrayTwo, arrayThree, and sumOne, sumTwo, sumThree. Removing 'static'from the Main and generateRandomNumbers methods removes all of the errors, but then of course it can't compile without the static Main method.

In my C# course, we are learning methods and arrays at the moment and the goal of this program is to put random numbers into three arrays and then display each number and the sum of the array.

I have searched around and the answers mostly go along the lines of "create an instance" but I'm not sure how to go about doing that. Any help would be much appreciated!

D Stanley
  • 149,601
  • 11
  • 178
  • 240
minusLeft
  • 11
  • 1
  • 1
  • 3
  • Side note please check out [MCVE] for guidance on creating better code samples. – Alexei Levenkov Feb 09 '16 at 20:30
  • Your `generateRandomNumbers` is a static function that belong to the "Class" while your arrayOne is a field that belongs to an "Instance". `public class Program {}` is a class. `var p = new Program()` is an instance of class "Program". It's like "Person" and "John" where John is an instance of Person. In your case, the easiest way is to make arrayOne...etc static as well. – BernieDADA Feb 09 '16 at 20:32
  • @Bjørn-RogerKringsjå I don't know. That is very good question for [META]. If you want to ask that make sure to search for existing posts there to clarify your point. – Alexei Levenkov Feb 09 '16 at 20:39

1 Answers1

4

When you stuff everything into Program it's usually easiest to just make everything static since Main needs to be static. So one option is to make your fields static:

static int[] arrayOne = new int[3];
static int[] arrayTwo = new int[5];
static int[] arrayThree = new int[7];
static int sumOne = 0;
static int sumTwo = 0;
static int sumThree = 0;

The other option is to make everything non-static create an instance of Program using the following syntax:

var program = new Program();

Then reference the properties and methods of that instance:

    //call method to generate numbers
    program.generateRandomNumbers();

    //call method to get sums of arrays
    program.sumOne = program.getSum(arrayOne);
    program.sumTwo = program.getSum(arrayTwo);
    program.sumThree = program.getSum(arrayThree);

As you can see, keeping everything static is easier unless you need to have properties that are different for each instance.

D Stanley
  • 149,601
  • 11
  • 178
  • 240