-1

I have problem with mz test ..Class1' does not contain a definition for 'Function' and no extension method 'Function' accepting a first argument of type 'Class1' could be found (are you missing a using directive or an assembly reference?) ClassTestProject2

and i have add reference to my class Class1 to the test library and objects are fine too here is my code i am new to c# so i probably didnt do something that i should do sobebodz has some ideas ? thank you :)

tests:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ClassLibrary1;

namespace ClassTestProject2
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethodPlus()
        {
            double number1 = 9.0;
            double number2 = 1.0;
            string op = "plus";
            double expected = 9.0;
            Class1 count = new Class1();
            double actual = count.Function(number1, number2, op);
            Assert.AreEqual(expected, actual);
        }
   }
}

here is my class:

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

namespace ClassLibrary1
{
    public class Class1
    {
        public static double Function(double num1, double num2, string op)
        {
        double finRes = 0;
        if (op == "plus")
        {
            finRes = num1 + num2;
        }
        else if (op == "minus")
        {
            finRes = num1 - num2;
        }
        else if (op == "multiple")
        {
            finRes = num1 * num2;
        }
        else if (op == "divide")
        {
            finRes = num1 / num2;
        }
        else if (op == "exp")
        {
            finRes = Math.Pow(num1, num2);
        }
        else if (op == "fac")
        {
            double result = num1;
            for (double i = (num1) - 1; i > 0; i--)
            {
                result = result * i;
                finRes = result;
            }
        }
        else if (op == "sqrt")
        {
            finRes = Math.Sqrt(num1);
        }
        return finRes;
    }
}
}
krakra
  • 35
  • 8

1 Answers1

3

You declared Function as static, meaning it can only be referenced as 'Class1.Function(...)`, but not from an instance of Class1.

Peter B
  • 22,460
  • 5
  • 32
  • 69
  • so what should i do ? – krakra Apr 22 '16 at 13:13
  • In this case, probably remove the `count` variable and just use `Class1.Function(...)`. Only if the instance has state (e.g. local variables) and/or behavior that depends on that, then it becomes useful to create an instance. – Peter B Apr 22 '16 at 13:15
  • I did the orrection but still it writes the error 'Class1' does not contain a definition for 'Function' – krakra Apr 22 '16 at 13:21
  • I just errase everything and did the cleaning in my directories adn now it looks life its fine thank you – krakra Apr 22 '16 at 13:43