-3

I'm facing the following error in C# Visual Studio that I just can't resolve:

"An object reference is required for the non-static field"

I don't know what it means nor how to fix it. Sure, I checked online but didn't find anything that spoke to my simple code below. All I want to do is call the following from Main() successfully:

int result = MaxPair(nums);

Many thanks in advance.


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

namespace MaxPairSpace
{
    class Program
    {

        static int Main(string[] args)
        {

            int n = Int32.Parse( Console.ReadLine() );

            string input = Console.ReadLine();
            string[] split = input.Split(' ');
            int length = split.Length;
            int[] nums = new int[length];
            for (int x = 0; x < length; x++)
            {
                nums[x] = Int32.Parse(split[x]);
            }

            int result = MaxPair(nums);

            Console.WriteLine("{0}\n", result);
            return 0;
        }

        public int MaxPair(int[] numbers)
        {
            int result = 0;
            int n = numbers.Length;
            for (int i = 0; i < n; ++i)
            {
                for (int j = i; j < n; ++j)
                {
                    if (numbers[i] * numbers[j] > result)
                    {
                        result = numbers[i] * numbers[j];
                    }
                }
            }
            return result;
        } 
    }
}
Bingfoot
  • 21
  • 1
  • 1
  • 6
  • 3
    Make `MaxPair` static. – noahnu May 17 '16 at 21:49
  • do a google search on the this and it will explain also do you know the difference between `Instance and Static` you should do some more reading on C# Basics if you want to gain access to the public method from within a static main then you should do `var prg = new Program()` for example then you can call `prg.MaxPair() method` otherwise change the `Method Signature to static` – MethodMan May 17 '16 at 21:50
  • Oh, makes sense. Thank you. – Bingfoot May 17 '16 at 22:07

1 Answers1

2

You cannot access a non-static method/Variable/Property directly inside a static method. To access such items you need to create an instance of the corresponding class and through that instance you can access them where as you can access other static methods/Variable/Property inside a static method. If such items belongs to a different class then You can access them through their class name. that is, ClassName.Method(). So better option here is to Change the method signature as like the following:

public static int MaxPair(int[] numbers)
{
   // Code here 
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • 1
    Thank you very much. I fully understand. Yes, I am a newbie here and most of my projects have been functionality implemented and called within classes. This time, it was supposed to be a simple method called from main(). But I get it. Thank you very much, this is solved now. – Bingfoot May 17 '16 at 22:08