How can I stop this error, here is my code and there error occurs where i have decimal.TryParse(....

 class EstCalc
 { 
    static  void Variables (string[] args)
    {

                decimal bl; //blank length
                decimal bw; //blank width
                decimal bp; //blank perifery            
                decimal pd; //part depth
                decimal dq; //die quantity
                decimal.TryParse(quantityTB.Text, out dq);
                decimal dl; //die length
                decimal dw; //die width
                decimal ds; //die shut height
                decimal d; //density
                decimal.TryParse(densityTB.Text, out d);

Dani AI

Generated

The compiler error "object reference required for non-static field" (CS0120) means a static method is trying to use an instance member (for example a form control). As pointed out, the static method cannot directly access instance fields or properties on the form. is also correct to ask for more context: whether Variables is being called from the form instance or from elsewhere determines the proper fix.

Three practical fixes:

  • Turn the method into an instance method so it can use this and the form's controls.
  • Keep the helper static but pass in the input strings (or parsed values) as parameters instead of reading controls directly.
  • If you must access the form instance from another class, call the method on that form instance (not from a static context).

A compact pattern that keeps parsing out of the UI and avoids static/instance mixups:

public static bool TryParseInputs(string textA, string textB, out decimal valueA, out decimal valueB)
{
    valueA = 0; valueB = 0;
    if (!decimal.TryParse(textA, out valueA)) return false;
    if (!decimal.TryParse(textB, out valueB)) return false;
    return true;
}

Notes and cautions:

  • Always check the boolean result of TryParse and handle invalid input.
  • Consider culture (decimal separator) when parsing; there are overloads that accept IFormatProvider.
  • If code runs on a background thread, Windows Forms controls must be accessed on the UI thread (see the docs for thread-safe calls).

See the C# static keyword docs for details on static vs instance behavior and the decimal.TryParse docs for parsing overloads: static keyword (C#) and decimal.TryParse. For cross-thread UI access, consult the WinForms threading guidance: Making thread-safe calls to Windows Forms controls.

Recommended Answers

All 4 Replies

What kind of error do you get?
Throwing code and saying "error" does not help very much.
decimal.TryParse works fine when I use it.

I was stated in the title "object reference required for nonstatic field.

You are right, but I can't infer from your code sample where the error is, because it is incomplete. And if possible please use code tags.

Your function is static but it's referring to variables that are part of instances.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.