-3

I am new to programming and am having trouble getting this piece of code to work. The fillPositive method should fill an array of size n with numbers 1-n in numerical order but when I try to call fillPositive from the main method I get the error at compile time 'The name 'fillPositive' does not exist in the current context'.

Here is my code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SOFT140
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] taskOneArray = new int[15];
            taskOneArray = fillPositive(15);
            Console.WriteLine(taskOneArray);
            Console.ReadKey();
        }

    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SOFT140
{
    public class ArrayMethod
    {
        public static int[] fillPositive(int n)
        {
            int sum = 0;               // Variable to hold element values
            int[] a = new int[n];

            for (int i = 0; i < a.Length; i++)       // Iterate though all array elements adding one each time
            {
                a[i] = sum + 1;
                sum++;
            }

            return a;   // Return the array

        }

    }
}

I have tried initialising the array in a few different ways to see if it was the problem. But I still get the same error.

int[] taskOneArray = fillPositive(15);

int[] taskOneArray = new int[fillPositive(15)];

Any help would be much appreciated.

EDIT: When I invoke the method like this:

taskOneArray = ArrayMethod.fillPositive(15);

I get the same error message for the class. 'The name ArrayMethod does not exist in the current context'

Nick
  • 150
  • 1
  • 12
  • 1
    Just name the class where the method resides: `ArrayMethod.fillPositive`. – Patrick Hofman Jan 29 '15 at 12:40
  • Also, no need to initialize an array you are instantly overwriting. (`int[] taskOneArray = new int[15];`) – Patrick Hofman Jan 29 '15 at 12:41
  • Your method is static, that is why you can not invoke it that way. Check this post for more information about static methods => http://stackoverflow.com/questions/4124102/whats-a-static-method-in-c – Dzyann Jan 29 '15 at 12:47

2 Answers2

1

you should call it like, as fillPositive is a static method in the class ArrayMethod.

static void Main(string[] args)
    {
        int[] taskOneArray = new int[15];
        taskOneArray = ArrayMethod.fillPositive(15);
        Console.WriteLine(taskOneArray);
        Console.ReadKey();
    }

You might understand a function call as visiting your friend's home. If you have to visit your friend's home outside your hometown, You need to first reach his hometown and then his home. In this context-

Main() method is your home.

ArrayMethod is his hometown &

fillPositive() is his home

Community
  • 1
  • 1
Rohit Prakash
  • 1,975
  • 1
  • 14
  • 24
  • Thanks for the reply. I did actually try that already but when I name the class I just get the same error message but for the class name instead. 'The name ArrayMethod does not exist in the current context' – Nick Jan 29 '15 at 14:10
  • @Nick, I don't think that should throw any error. Syntax and syntactic all are valid in this code. – Rohit Prakash Jan 30 '15 at 04:27
0

Long story short: You forgot to use the function from the class.

The function fillPositive exists in the ArrayMethod class, and the Program class is not familiar with it. Thus, you need to make the call into the class in order to get the relevant method from it.

This is done via the following syntax:

<Class name>.<Method>(<Parameters>)

or in your case, instead of:

int[] taskOneArray = fillPositive(15);

This:

int[] taskOneArray = ArrayMethod.fillPositive(15);
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
A. Abramov
  • 1,823
  • 17
  • 45