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'