0

I have made a function called "AddNumbers" but when I call it, I get an error that says "An object reference is needed". I have placed this function outside of the Main() entry point, but I am sure it has nothing to do with placement because I tried moving it and it would still not work.

namespace FunctionPractice
{
     class Program
     {
        public int AddNumbers(int number1, int number2)
        {
            int result = number1 + number2;
            return result;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Enter 2 numbers: ");
            int firstNumber = Convert.ToInt16(Console.ReadLine());
            int secondNumber = Convert.ToInt16(Console.ReadLine());

            int result = AddNumbers(firstNumber, secondNumber);
            Console.WriteLine(result);
        }
    }
}
hardkoded
  • 18,915
  • 3
  • 52
  • 64

4 Answers4

0

Si main is static, AddNumbers must be static as well.

hardkoded
  • 18,915
  • 3
  • 52
  • 64
0

Hey your method works fine you just need to make your "AddNumbers" method static. You can't call a non-static method from a static one. Also you may want to add a read line to the end so the program doesn't just shut off once it writes to the screen. Here would be the fixed code.

class Program
{
    public static int AddNumbers(int number1, int number2)
    {
        int result = number1 + number2;
        return result;
    }

    static void Main(string[] args)
    {
        Console.WriteLine("Enter 2 numbers: ");
        int firstNumber = Convert.ToInt16(Console.ReadLine());
        int secondNumber = Convert.ToInt16(Console.ReadLine());

        int result = AddNumbers(firstNumber, secondNumber);
        Console.WriteLine(result);
        Console.ReadLine();
    }
}
Kieran
  • 13
  • 5
0

Make your function as public static int AddNumbers(int number1, int number2).

This is because, you are calling your function from a static function which belongs to the class itself. Your function belongs to the objects of the class. So when you call static function main(), it doesn't know of which instance's AddNumbers to call. Hence the error. Please read more about static keyword.

Mangesh
  • 5,491
  • 5
  • 48
  • 71
0

Change the method declaration to

 public static int AddNumbers(int number1, int number2)

Or create an instance of Program class in main method and then access the add numbers method.

 namespace FunctionPractice
 {
      class Program
      {
         public int AddNumbers(int number1, int number2)
         {
             int result = number1 + number2;
             return result;
         }

         static void Main(string[] args)
         {
             Console.WriteLine("Enter 2 numbers: ");
             int firstNumber = Convert.ToInt16(Console.ReadLine());
             int secondNumber = Convert.ToInt16(Console.ReadLine());

            Program program = new Program();

            int result = program.AddNumbers(firstNumber, secondNumber);
            Console.WriteLine(result);
        }
    }
}
bluekushal
  • 499
  • 6
  • 14
  • Interesting way to solve the problem. –  Jun 09 '17 at 02:55
  • And also instead on Convert.ToInt16, try using Int16.TryParse. You can avoid throwing unnecessary exception by using TryParse. – bluekushal Jun 09 '17 at 03:02
  • what is the difference between the conversion and the Prase? What does the Parse do? –  Jun 09 '17 at 03:04
  • Parsing means **analyzing**. Convert.ToInt16 will throw exception if conversion fails. TryParse will never throw exception. Read [this](https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx). – bluekushal Jun 09 '17 at 03:09
  • Awesome, I will start using that. Good read, thanks –  Jun 09 '17 at 03:12
  • Mo Mehdi, **choose one answer as correct answer**. You'll gain more reputation by doing so and on stackoverflow reputation is directly linked to privileges. – bluekushal Jun 09 '17 at 03:20
  • No problem, I put yours as the good answer. –  Jun 09 '17 at 03:30