简体   繁体   中英

Object reference for non-static field in c#

I had created a function in c# as:

public void input_fields(int init_xcor, int init_ycor, char init_pos, string input)
{
    char curr_position = 'n';           
    foreach (char c in input)
    {           
        if (c == 'm')
        {
            Move mv = new Move();
            if (curr_position == 'e' || curr_position == 'w')
            {
                init_xcor = mv.Move_Step(curr_position, init_xcor, init_ycor);
            }
            else
            {
                init_ycor = mv.Move_Step(curr_position, init_xcor, init_ycor);
            }
        }
    }
}

and I am calling the function as:

input_fields(init_xcor, init_ycor, init_pos, input);

but while calling it is giving an error:

An object reference is required for the non-static field, method, or property 'TestProject.Program.input_fields(int, int, char, string)' xxx\TestProject\Program.cs 23 17 TestProject

I don't want to make the function static as I have to do an unit test also..

What should I do for this? ... Please help me out.

You have to create an instance of the class containing this method in order to access the method.

You can't simply execute methods in the way it seems you're trying.

MyClass myClass = new MyClass();
myClass.input_fields(init_xcor, init_ycor, init_pos, input);

You can create methods as static so that you can access them without the instantiation of an object however you still need to refer to the class name.

public static void input_fields(int init_xcor, int init_ycor, 
                                                  char init_pos, string input)

and then

MyClass.input_fields(init_xcor, init_ycor, init_pos, input);

You must call this method on an object of the class where this method belongs. So if you have something like:

public class MyClass
{
     public void input_fields(int init_xcor, int init_ycor, char init_pos, string input)
     {
         ...
     }
     ...
}

You must do:

MyClass myObject = new MyClass();
myObject.input_fields(init_xcor, init_ycor, init_pos, input);

Since the function is not static , you need to create the instance of the class to invoke the method, for example,

MyClass cl = new MyClass();
cl.input_fields(init_xcor, init_ycor, init_pos, input);

else mark the method as static like

public static void input_fields....

and call it like MyClass.input_fields(init_xcor, init_ycor, init_pos, input);

From your error it looks like you're calling your method from main() or some other static method in the static Program class. Simply declaring your method as static will fix the issue:

public static void input_fields(int init_xcor, int init_ycor, char init_pos, string input)  ...

But this is a quick fix and probably not the best solution (unless you're simply prototyping or testing a feature.) If you plan on working this code out further your method should be moved into a separate class (static or not.) The Program class and its main() function are meant as an entry point into the application and not the sole location of your application's logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM