无法使用实例引用访问

时间:2014-03-15 22:22:41

标签: c#

我仍然在编程任务的中间尝试使用方法来制作计算器,我得到了:

Member 'LabAssignFiveCalc.Sum.Addition(double, double)' cannot be accessed with an instance reference; qualify it with a type name instead  

在这行代码上

        Sum s = new Sum();
        if (metUse == "Addition")
            result = s.Addition(num1, num2);

这是整个文件

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

namespace LabAssignFiveCalc
{
   public class Sum
    {
        //Addition method
        public static double Addition(double num1, double num2)
        {
            double result;
            result = num1 + num2;
            return result;
        }
        //Subtraction method
        public static double Subtract(double num1, double num2)
        {
            double result;
            result = num1 - num2;
            return result;

        }
        //Multiplication method
        public static double Multiply(double num1, double num2)
        {
            double result;
            result = num1 * num2;
            return result;
        }
        //Division method
        public static double Divide(double num1, double num2)
        {
            double result;
            result = num1 / num2;
            return result;

        }
    }
    public class Program
    {

        //Main
        static void Main(string[] args)
        {
            //Declare Variables
            int choice;
            string op;
            double num1;
            double num2;
            double result;
            string metUse;
            //Ask user for calculation parameters
            Console.WriteLine("Calculator:");
            Console.WriteLine("Which operation do you wish to perform? Type the corresponding number.");
            Console.WriteLine("1) Addition\n2) Subration\n3) Multiplication\n4) Division");
            choice = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please type the first number you wish to calculate");
            num1 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Please type the second number you wish to calculate");
            num2 = Convert.ToDouble(Console.ReadLine());
            //Declare Operator
            switch (choice)
            {
                case 1:
                    op = "+";
                    metUse = "Addition";
                    break;
                case 2:
                    op = "-";
                    metUse = "Subtract";
                    break;
                case 3:
                    op = "*";
                    metUse = "Multiply";
                    break;
                case 4:
                    op = "/";
                    metUse = "Divide";
                    break;
                default:
                    throw new ArgumentException();
            }
            Sum s = new Sum();
            if (metUse == "Addition")
                result = s.Addition(num1, num2);

            //Show Calculation
            Console.WriteLine("{0}{1}{2}={3}", num1, op, num2, result);
            Console.ReadKey();
        }
    }
}

就像我说的那样,我仍然是一个初学者,所以请放轻松对待我:P这可能是一些真正的菜鸟错误。

感谢您的帮助:D

3 个答案:

答案 0 :(得分:2)

Addition是一种静态方法。

您不能使用类型的实例调用静态方法;用类型本身来称呼它。

请改为尝试:

if (metUse == "Addition")
    result = Sum.Addition(num1, num2);

答案 1 :(得分:1)

Sum类的Addition成员标记为static。这意味着它无法在实例化的类上调用。这样做:

result = Sum.Addition(num1, num2);

答案 2 :(得分:-2)

由于加法是Sum类中的静态方法,因此可以将其作为sum.addition(a,b)进行访问; 而且你不必创建一个对象来使用它。