I'm trying to call a function but keep getting an error saying it doesn't exist in this current context. It's for a class assignment. I'm not sure why it's not recognizing the function.

using System;               
public class Program
{

    public int W(int a, int b)
    {   
        return 0;   
    }

    public static void Main()   
    {
        int j= W(1,1);
    }
}

2 Answers

2
Michael Randall On

What you are seeing is

Compiler Error CS0120

An object reference is required for the nonstatic field, method, or property 'member'

In order to use a non-static field, method, or property, you must first create an object instance.

Solution

Change

public int W(int a, int b)

to

public static int W(int a, int b)

Basically, you cant call instance members from a static method without an instance


Further reading

static (C# Reference)

Static Classes and Static Class Members (C# Programming Guide)

Instance Constructors (C# Programming Guide)

0
mohammad dardaei On

in static method you must use static member otherwise not found in current context

so you must type static before your type(int) and then call it