在非静态上下文中无法访问静态方法 [英] Cannot access static method in non-static context

查看:84
本文介绍了在非静态上下文中无法访问静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始了C#编程的中级课程,并且正在学习如何创建多个类以及创建要在程序中使用的方法.

I've just recently started my intermediate course in C# programming and I'm learning how to create multiple classes and creating methods to use in my program.

对我来说这是一个非常新的话题,对于这很明显或很愚蠢,我深表歉意.我在所有方法中都收到了以下消息:

It's a very new topic to me so I apologize if it's something very obvious or stupid. I am getting below message in all of my methods:

Cannot access static method in non-static context

方法类中的代码

public static int Add(params int[] numbers) {
        var sum = 0;

        foreach (var n in numbers) {
            sum += n;
        }

        return sum;
    }

    public static int Subtract(params int[] numbers) {
        var sum = 0;

        foreach (var n in numbers) {
            sum -= n;
        }

        return sum;
    }

    public static int Multiply(params int[] numbers) {
        var sum = 0;

        foreach (var n in numbers) {
            sum *= n;
        }

        return sum;
    }

    public static int Divide(params int[] numbers) {
        var sum = 0;

        foreach (var n in numbers) {
            sum /= n;
        }

        return sum;
    }

    public static string[] CheckingOfSomeSort(string userInput, int value, bool isAddition, bool isSubtraction, bool isDivision, bool isMultiplication) {
        if (userInput.Contains("+")) {
            var addition = userInput.Split('+');
            value = 1;
            isAddition = true;
            return addition;
        } else if (userInput.Contains("-")) {
            var subtraction = userInput.Split('-');
            value = 2;
            isSubtraction = true;
            return subtraction;
        } else if (userInput.Contains("*")) {
            var multiplication = userInput.Split('*');
            value = 3;
            isMultiplication = true;
            return multiplication;
        } else if (userInput.Contains("/")) {
            var division = userInput.Split('/');
            value = 4;
            isDivision = true;
            return division;
        }

        return null;
    }

我正在尝试创建一个计算器(我已经做过,但是现在我正在尝试使用方法)

I am attempting to create a calculator (which I have already done, however I'm trying to use methods now)

推荐答案

根据您的评论,我知道您正在创建CalculatorMethods的对象,并且您正在尝试使用该方法调用该类的静态方法对象.

As per your comment I got to know that you are creating an object of CalculatorMethods and you are trying to call methods of that class which are static using this object.

我对问题的评论:

这些方法是静态的. (以及它们的使用方式,它们也应该是静态的).但是不能使用Class的对象而是直接使用Class的类型来访问静态方法.在这里,我猜想CalculatorMethods是其中包含方法的类,您将尝试执行calc.Add()..之类的操作.而是使用CalculatorMethods.Add()

these methods are static. (and the way they are used they should be static too). But static methods cannot be accessed with object of Class but with directly Type of the class. here I m guessing CalculatorMethods is class in which methods are and you will try to do something like calc.Add() .. which will not be possible . Instead do CalculatorMethods.Add()

相反,您可以像belwo一样直接通过Type调用来尝试使用它,

Instead you can try it by calling with Type directly like belwo,

    void MethodOfCalling()
    {
        int sum = CalculatorMethods.Add(new int[2] { 1, 2 });
    }

您可以看到,我使用了CalculatorMethods(一个类名-更正确地说是类的类型")来调用方法而不是该类的对象.

you can see, I have used CalculatorMethods (a class name - more properly saying Type of the class) to call method not object of the class.

这篇关于在非静态上下文中无法访问静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆