简体   繁体   English

非静态字段,方法或属性ConsoleApplication5.Program.myFirst(int,int)要求对象引用

[英]An object reference is required for the non-static field, method, or property ConsoleApplication5.Program.myFirst(int, int)

I am new to C# and programming at whole and today I started learning C#. 我是C#和整体编程的新手,今天我开始学习C#。 I have reached in the function part of my tutorial book but I fail to grasp the error in my code. 我已经达到了教程书的功能部分,但是无法理解代码中的错误。 I am doing exactly what's written on the book and yet this error is popping out. 我正在做的完全是书上写的内容,但是这个错误却突然出现了。 Google couldn't help me much as most of the Google search results had complicated solutions which went above my head. Google帮不了我什么,因为大多数Google搜索结果中都有复杂的解决方案,这些问题超出了我的脑海。 So, I would appreciate if someone takes a few minutes and point me out what I am doing wrong in my code. 因此,如果有人花几分钟时间指出我在代码中做错的事情,我将不胜感激。

The IDE is showing this error: IDE显示此错误:

Error 1 An object reference is required for the non-static field, method, or property 'ConsoleApplication5.Program.myFirst(int, int)' C:\\Users\\Eion\\documents\\visual studio 2013\\Projects\\ConsoleApplication5\\ConsoleApplication5\\Program.cs 17 25 ConsoleApplication5 错误1非静态字段,方法或属性'ConsoleApplication5.Program.myFirst(int,int)'需要对象引用C:\\ Users \\ Eion \\ documents \\ visual studio 2013 \\ Projects \\ ConsoleApplication5 \\ ConsoleApplication5 \\ Program .cs 17 25 ConsoleApplication5

And my code is: 我的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("First: ");
            int ab = int.Parse(Console.ReadLine());
            Console.Write("Second: ");
            int ba = int.Parse(Console.ReadLine());
            int my1Res= myFirst(ab, ba);
            Console.WriteLine("The result is " + my1Res);

        }
        public int myFirst(int ab, int ba)
        {
            int myRes = ab + ba;
            return myRes;
        }
    }
}

You need to make myFirst method static. 您需要将myFirst方法myFirst静态。

public static int myFirst(int ab, int ba)

You can't call a non-static method from a static context without an instance. 没有实例,就不能从静态上下文中调用非静态方法。

See Compiler Error CS0120 for more details. 有关更多详细信息,请参见编译器错误CS0120

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误1非静态字段,方法或属性'temperature_conversion.Program.Celsius(int)'需要对象引用 - Error 1 An object reference is required for the non-static field, method, or property 'temperature_conversion.Program.Celsius(int)' 非静态字段,方法或属性DoStuff(int,int)'需要对象引用 - An object reference is required for the non-static field, method, or property DoStuff(int, int)' GetAbbreviatedMonthName中的非静态字段,方法或属性需要对象引用 - An object reference is required for the non-static field, method, or property in GetAbbreviatedMonthName 非静态字段,方法或属性需要对象引用 - An object reference is required for the non-static field, method, or property “非静态字段,方法或属性需要对象引用” - “An object reference is required for the non-static field, method, or property” 非静态字段,方法或属性需要对象引用 - An object reference is required for the non-static field, method, or property 非静态字段,方法或属性所需的对象引用 - Object reference required for non-static field, method, or property 非静态字段,方法或属性错误需要对象引用 - An object reference is required for the non-static field, method or property error 非静态字段,方法或属性需要对象引用 - An object reference is required for the non-static field, method, or property 非静态字段,方法或属性需要对象引用 - Object reference is required for non-static field, method, or property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM