Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

c# - Cannot access static method in non-static context

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

Code in method class:

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

My Comment on question:

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()

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...