Charles Han
Charles Han

Reputation: 799

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

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

Upvotes: 3

Views: 25918

Answers (5)

Laurie Stearn
Laurie Stearn

Reputation: 999

This solution is exactly the one sought here. Trying to pass an object from Static Main into a method in Class: Program is well nigh impossible without:

 `Program ProgramInstance = new Program();
 ProgramInstance.ProcessObject(MyObject);`

Upvotes: 0

Bogdan Alexandru
Bogdan Alexandru

Reputation: 5552

If you don't want to change them to static, then simply move them to another class, and then inside Main create an object and use the functions.

Upvotes: -1

Adam Rackis
Adam Rackis

Reputation: 83376

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.

Upvotes: 4

LightStriker
LightStriker

Reputation: 21024

It is because Main, being static, is not assigned to any instance of Problem2. Not knowing at which instance of Problem2 to send the variable, it is unable to call the right method.

Right now, your method doesn't modify any fields of any Problem2. But they could, and that's the whole problem. Which instance of Problem2 should they modify? The static method has no way to know that.

So, if you had initialized an instance of Problem2, you could call its own version of the method from the static Main.

Problem2 problem = new Problem2();

problem.cadToWon(...)

Upvotes: 0

codingbiz
codingbiz

Reputation: 26396

Making your methods static will solve that problem. You cannot call an instance member (not preceded with static) from a static method (e.g. static void main(..)). It either both of them have to be declared static or you create the instance of the class in which the methods are and access them through the instance. I don't think you need that here

static double cadToWon( double x )
{
    return x * 1113.26;
}

static double wonToCad( double x)
{
    return x / 1113.26;
}

Accessing through instance

Program2 p2 = new Program2();
double x = p2.wonToCad(10);

Upvotes: 0

Related Questions