Austen
Austen

Reputation: 293

Cannot be accessed with instance reference

I'm still in the middle of my programming assignment trying to use methods to make a calculator, and I am getting:

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

on this line of code

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

Here's the entire file

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

Like I said, I'm still a beginner so go easy on me please :P It's probably some really rookie error.

Thanks for the help :D

Upvotes: 1

Views: 665

Answers (3)

J0e3gan
J0e3gan

Reputation: 8938

Addition is a static method.

You cannot call a static method using an instance of a type; call it with the type itself.

Try this instead:

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

Upvotes: 2

AGM
AGM

Reputation: 21

As the addition is a static method in Sum class, then it can be accessed as sum.addition(a,b); and you don't have to create an object to use it.

Upvotes: -2

Andreas
Andreas

Reputation: 1081

The Addition member of the Sum class is marked as static. This means it cannot be called on an instantiated class. Do this instead:

result = Sum.Addition(num1, num2);

Upvotes: 1

Related Questions