C#: An object reference is required for the non-static field, method, or Property

25,031

Since your Main method is static, cadToWon and wonToCad also have to be static if you want to call them from Main.

static double cadToWon(double x) //...


static double wonToCad(double x) //...

The other option would be to break all of the logic of your Main, cadToWon, and wonToCad methods out into a new class, and then have you Main method simply set up and run that new class. But I suspect that might be beyond the scope of your assignment.


To answer you question of why adding static makes this work:

static methods are shared across all instances of a class. So no matter what instance of class Problem2 you're in, there's only one Main method that's shared across all of them.

cadToWon, however, is an instance method. It belongs to a particular instance of class Problem2.

As a result, you can't call cadToWon from Main, since Main doesn't know what instance of Problem2 to call cadToWon on. Main doesn't know what instance to call cadToWon on since Main doesn't belong to any instance.

Share:
25,031

Related videos on Youtube

Charles Han
Author by

Charles Han

Updated on March 30, 2020

Comments

  • Charles Han
    Charles Han about 4 years

    I feel bad for asking this when there are so many questions that are related but I was not able to find/understand the answer I am looking for.

    // 2. Develop a program to convert currency X to currency Y and visa versa.

    using System;
    
    class Problem2
    {
        static void Main (string[] args)
        {
            while (true) {
                Console.WriteLine ("1. Currency Conversion from CAD to Won");
                Console.WriteLine ("2. Currency Conversion from Won to Cad");
                Console.Write ("Choose from the Following: (1 or 2)? ");
                int option = int.Parse( Console.ReadLine() );
                //double x;
                if (option == 1) {
                    Console.WriteLine ("Type in the amount you would like to Convert CAD to Won: ");
                    //double y =double.Parse( Console.ReadLine());
                    //Console.WriteLine( cadToWon( y ) );
                    Console.WriteLine( cadToWon( double.Parse( Console.ReadLine() ) ));
                }
                if (option == 2) {
                    Console.WriteLine ("Type in the amount you would like to Convert Won to CAD: ");
                    Console.WriteLine( wonToCad (double.Parse( Console.ReadLine())));
                }
            }
        }
    
        double cadToWon( double x )
        {
            return x * 1113.26;
        }
    
        double wonToCad( double x)
        {
            return x / 1113.26;
        }
    }
    

    This give me the Error messgae "An object reference is required for the non-static field, method, or property 'Problem2..." I know that I'll be able to run the program if I add static infront of the methods but I'm wondering why I need it (I think it's because Main is static?) and what do I need to change in order to use these methods without adding static to them?

    Thank you

    • LightStriker
      LightStriker over 11 years
      Kinda funny that everybody didn't answer the question and went directly to "Make them static" while the question was why and how to do it without static?
    • Charles Han
      Charles Han over 11 years
      Yeah lol but people heard you and included an explanation. Thanks!
  • LightStriker
    LightStriker over 11 years
    "I know that I'll be able to run the program if I add static infront of the methods but I'm wondering why I need it"
  • LightStriker
    LightStriker over 11 years
    They don't HAVE to be static, and they don't HAVE to be in another class. I admit it's a better design, but it is not a constraint.
  • Adam Rackis
    Adam Rackis over 11 years
    @Marc - of course you're right. He could create a new instance of the class Problems2, and call cadToWon on that instance from within Main. I just want to keep things simple for OP.
  • LightStriker
    LightStriker over 11 years
    He knows that making them static work... His question was why and how to make it work without static? Kinda wierd that everybody answer "make them static" to that question.
  • Adam Rackis
    Adam Rackis over 11 years
    @Marc - indeed. Thank you for pointing that out. I added an explanation for him.
  • codingbiz
    codingbiz over 11 years
    You needed it because main is static. My answer includes how to not add static and still call the methods - creating instance of Program2
  • Charles Han
    Charles Han over 11 years
    Thank you both Adam and Marc-André