Scenario:
Consider the following code:
private static void SumData(object state) { // Calling a non-static member from a static method setTextboxText(result); }
Explanation:
The error "CS0120" occurs when you attempt to access a non-static member (field, method, or property) from a static context. In this case, the setTextboxText method is a non-static member of the Form1 class, and it cannot be accessed from the static SumData method.
Possible Solutions:
static void setTextboxText(int result) { // Implementation details }
class Form1 { public static Form1 It; public Form1() { It = this; } private static void SumData(object state) { Form1.It.setTextboxText(result); } }
private static void SumData(Form1 form, object state) { form.setTextboxText(result); }
private void SumData(object state) { setTextboxText(result); }
Additional Information:
The above is the detailed content of Why Does 'CS0120: An object reference is required...' Occur When Calling a Non-Static Method from a Static Method?. For more information, please follow other related articles on the PHP Chinese website!