Cs0120: an Object Reference Is Required For the Nonstatic Field, Method, or Property 'Foo'

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.

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 non-static field, method or property

Your B constructor should look like this:

 public B(Property p1, Property p2): base(p1)

So B accepts p1 and p2, and p1 is passed to the base constructor in A.

Error CS0120: An object reference is required for the non-static field, method, or property 'InventoryUI.newDiamondText'

It means the field (or method or whatever it is) newDiamondText isn’t static but you’re trying to call it using the static reference to the class InventoryUI rather than via a reference to an instance of that class.

If you meant newDiamondText to be static — i.e. there’s only one of it ever, not one for each copy of the class — you can solve the problem by marking newDiamondText as static.

Otherwise, you’ll need to access it via a reference to an instance of InventoryUI — e.g. something like (but probably not exactly) this:

InventoryUI inventoryInstance = new InventoryUI();
inventoryInstance.newDiamondText.text = playerInventory.NumberOfDiamonds.ToString();


Related Topics



Leave a reply



Submit