1

I am creating a really basic program, that has a method to fill an array but I am getting an error I don't understand. I am a Java programmer trying to acclimate to C# and .NET. Any help would be great.

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

namespace TestThreads
{
    class Program
    {
        static void Main(string[] args)
        {
             int[]  empty = new int[50001];
             int[] filled = new int[50001];

            filled = compute(empty); //error occurs here

            Console.ReadLine();
        }

        public int[] compute(int[] inArray)
        {
            for (int i = 0; i < inArray.Length; i++)
            {
                inArray[i] = i << 2;
            }

            return inArray;
        }
    }
}

Error Message:

Error 1 An object reference is required for the non-static field, method, or property 'TestThreads.Program.compute(int[])' C:\Users\hunter.mcmillen\Desktop\TestProcessSplitting\TestProcessSplitting\Program.cs 17 22 TestThreads

Thanks, Hunter

2
  • Java would give you a similar error in that case... you're trying to call an instance method as if it was static. Commented Jun 3, 2011 at 14:55
  • correct, but the Java compiler gives me this message: non-static method compute(int[]) cannot be referenced from a static context, which I can actually understand. Commented Jun 3, 2011 at 14:57

6 Answers 6

6

The compute method should be static.

public static int[] compute(int[] inArray)
Sign up to request clarification or add additional context in comments.

Comments

6

You're trying to call compute which is an instance method from Main which is a static method. To fix this make compute static as well`

public static int[] compute(int[] inArray) 

Comments

2

Main is a static method - it is not specific to any single object - indeed, no instance of Program is created to call Main. compute is an instance method, and needs to be invoked on a single object.

Two options:

  1. make compute static, which makes sense since it uses no state (fields):

    public static int[] compute(int[] inArray) {...}
    
  2. create an instance in Main:

    var obj = new Program();
    filled = obj.compute(empty); 
    

The first is more appealing here. I've included the second purely for completeness.

Comments

1

Change public int[] compute(int[] inArray){...}

to

public static int[] compute(int[] inArray){..}

or change your call from

filled = compute(empty); 

to

filled = new Program().compute(empty); 

The compute() method that you have is an instance (non-static) method and requires an instance to be invoked.

Comments

0

Add static to the compute method declaration.

public static int[] compute(int[] inArray)

Comments

0

You're method is not static, but is being referenced from a static method. Change it to static. Solved.

public static int[] compute(int[] inArray) { ... }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.