C# Error: "An Object Reference Is Required For the Non-Static Field, Method, or Property"

CS0120: An object reference is required for the nonstatic field, method, or property 'foo'

It looks like you are calling a non static member (a property or method, specifically setTextboxText) from a static method (specifically SumData). You will need to either:

  1. Make the called member static also:

    static void setTextboxText(int result)
    {
    // Write static logic for setTextboxText.
    // This may require a static singleton instance of Form1.
    }
  2. Create an instance of Form1 within the calling method:

    private static void SumData(object state)
    {
    int result = 0;
    //int[] icount = (int[])state;
    int icount = (int)state;

    for (int i = icount; i > 0; i--)
    {
    result += i;
    System.Threading.Thread.Sleep(1000);
    }
    Form1 frm1 = new Form1();
    frm1.setTextboxText(result);
    }

    Passing in an instance of Form1 would be an option also.

  3. Make the calling method a non-static instance method (of Form1):

    private void SumData(object state)
    {
    int result = 0;
    //int[] icount = (int[])state;
    int icount = (int)state;

    for (int i = icount; i > 0; i--)
    {
    result += i;
    System.Threading.Thread.Sleep(1000);
    }
    setTextboxText(result);
    }

More info about this error can be found on MSDN.

An object reference is required for a non-static field, method, or property

Your Main method is static and you can access only static members of the same class from a static method. All you need to do to make your EasyExploits.Module static as well:

private static readonly EasyExploits.Module module = new EasyExploits.Module();

C# error: An object reference is required for the non-static field, method, or property

The Main method is Static. You can not invoke a non-static method from a static method.

GetRandomBits()

is not a static method. Either you have to create an instance of Program

Program p = new Program();
p.GetRandomBits();

or make

GetRandomBits() static.

C# classes: An object reference is required for the non-static field, method, or property

You've got two options, make test static (that means it doesn't need an instance of TestClass to exist) or create an instance of testClass

{
// class 1
class TestClass
{
public string nonStaticTest = "Test";//value assigned when object created
public static string staticTest = "Test";//value assigned at the start of runtime
}
class Program
{
static void Main(string[] args)
{
Console.writeline(TestClass.staticTest);//this is fine
var instanceOfClass1 = new TestClass();
Console.writeline(instanceOfClass1.nonStaticTest);//this is also fine

{
}
}

an object reference is required for the non-static field method or property 'page.request'

Your issue here is that you made this method static. Static means that it's not tied to a specific instance. The Request property that this code is referring to is an instance property. This means that this property (can) have a different value for each instance.

ASP.NET will create a new instance of this class for each request that enters, this will make it easier to share information like the Request, Response, Context, ... for a specific request, so you have to think less about tying this all together.

C# An object reference is required for the nonstatic field


Why am I getting this error even when I don't have anything static?

You're getting this error because you don't have anything static. You're trying to use a static method that you've defined as an instance method. Right here:

RegistrationFunctions.GotoStep(this, 1);

You're not invoking it from an instance, you're trying to statically invoke it from the class. You have two options:

You can make it static:

public static void GotoStep(RegistrationWindow window, int step)
{
//...
}

or you can create an instance of your class and call the method on that instance:

var functions = new RegistrationFunctions();
functions.GotoStep(this, 1);

Which way is correct is really up to you as you define the semantics of your program and decide what makes sense to be static and what doesn't.



Related Topics



Leave a reply



Submit