1

I have a static array and I need to pass an arbitrary element of it to a non-static method.

How do I do that?

public class MyClass
{
    public static int[] staticArray = { 3, 11, 43, 683, 2731 };

    public void SomeMethod(int value)
    {
        //...stuff...
    }

    public static void staticMethod()
    {
         SomeMethod(staticArray[2]);    //error here
    }
}

When I try something like that I get the error An object reference is required for the non-static field, method, or property.

10
  • 3
    The above code compile (if you replace ...stuff... with // ...stuff.... Commented Mar 2, 2013 at 14:51
  • 1
    Your code works fine for me in LinqPad. You must have simplified your test too much. Commented Mar 2, 2013 at 14:51
  • 1
    That code compiled perfectly fine.. paste the rest of it.. the error must be somewhere else in ...stuff.... or somewhere... Commented Mar 2, 2013 at 14:51
  • 2
    Runs fine for me used a breakpoint and 43 was in value as expected. Commented Mar 2, 2013 at 14:53
  • 1
    @Albin, the OP did not have trouble compiling. Commented Mar 2, 2013 at 14:55

1 Answer 1

6

Your code as it is is fine, however 'An object reference is required for the non-static field, method, or property' occurs when you try to call instance method, or access a non-static field/property on something other than an instance of a class, say from a static method. For example:

class MyClass
{
    private int imNotStatic;

    public static void Bar()
    {
        // This will give you your 'An object reference is required` compile 
        // error, since you are trying to call the instance method SomeMethod
        // from a static method, as there is no 'this' to call SomeMethod on.
        SomeMethod(5);

        // This will also give you that error, as you are calling SomeMethod as
        // if it were a static method.
        MyClass.SomeMethod(42);

        // Again, same error, there is no 'this' to read imNotStatic from.
        imNotStatic = -1;
    }

    public void SomeMethod(int x)
    {
        // Stuff
    }
}

Ensure that you are not doing one of the above. Are you sure you are calling SomeMethod from a constructor?

Sign up to request clarification or add additional context in comments.

6 Comments

I appear to be doing exactly that... plz see my edited original post. So, how can I do what I need without the error? Dave
You can't, SomeMethod would need to be static for you to call it from a static method. Please refresh on the difference between static and instance methods, and hopefully that should give you insight into the correct way to solve your actual problem.
However, note that from within SomeMethod, you would have access to staticArray, which means instead of // stuff you could write int value = staticArray[2];
but if I can't call SomeMethod from staticMethod I can't get to int value = staticArray[2]; when another form calls staticMethod. The aim here is to allow other forms in the app to use MyClass.staticMethod(); to run SomeMethod which does stuff to MyClass via non-static props/methods.
That aim implies a misunderstanding of how static and instances differ. Say in your program you had MyClass a; MyClass b; When you call MyClass.staticMethod(); what instance would it call SomeMethod on? a, b? From within a static method there is no instance of MyClass to call non-static props/methods on
|

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.