Error: “an object reference is required for the no

2018-12-31 15:12发布

I'm creating an application in C#. Its function is to evaluate if a given is prime and if the same swapped number is prime as well.

When I build my solution in Visual Studio, it says that "an object reference is required for the non-static field, method or property...". I'm having this problem with the "volteado" and "siprimo" methods.

Where is the problem and how can I fix it?

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Write a number: ");
            long a= Convert.ToInt64(Console.ReadLine()); // a is the number given by the user


            long av = volteado(a); // av is "a" but swapped

            if (siprimo(a) == false && siprimo(av) == false)
                Console.WriteLine("Both original and swapped numbers are prime.");
            else
                Console.WriteLine("One of the numbers isnt prime.");
            Console.ReadLine();
        }

        private bool siprimo(long a)
        {
            // Evaluate if the received number is prime
            bool sp = true;
            for (long k = 2; k <= a / 2; k++)
                if (a % k == 0) sp = false;
            return sp;
        }

        private long volteado(long a)
        {
            // Swap the received number
            long v = 0;
            while (a > 0)
            {
                v = 10 * v + a % 10;
                a /= 10;
            }
            return v;
        }
    }
}

标签: c#
6条回答
旧时光的记忆
2楼-- · 2018-12-31 15:31

Create a class and put all your code in there and call an instance of this class from the Main :

static void Main(string[] args)
{

    MyClass cls  = new MyClass();
    Console.Write("Write a number: ");
    long a= Convert.ToInt64(Console.ReadLine()); // a is the number given by the user
    long av = cls.volteado(a);
    bool isTrue = cls.siprimo(a);
    ......etc

}
查看更多
何处买醉
3楼-- · 2018-12-31 15:36

You just need to make the siprimo and volteado methods static.

private static bool siprimo(long a)

and

private static long volteado(long a)
查看更多
像晚风撩人
4楼-- · 2018-12-31 15:41

Change your signatures to private static bool siprimo(long a) and private static long volteado(long a) and see where that gets you.

查看更多
时光乱了年华
5楼-- · 2018-12-31 15:46

You can't access non-static members from a static method. (Note that Main() is static, which is a requirement of .Net). Just make siprimo and volteado static, by placing the static keyword in front of them. e.g.:

 static private long volteado(long a)
查看更多
像晚风撩人
6楼-- · 2018-12-31 15:52

The error message means that you need to invoke volteado and siprimo on an instance of the Program class. E.g.:

...
Program p = new Program();
long av = p.volteado(a); // av is "a" but swapped

if (p.siprimo(a) == false && p.siprimo(av) == false)
...

They cannot be invoked directly from the Main method because Main is static while volteado and siprimo are not.

The easiest way to fix this is to make the volteado and siprimo methods static:

private static bool siprimo(long a)
{
    ...
}

private static bool volteado(long a)
{
   ...
}
查看更多
还给你的自由
7楼-- · 2018-12-31 15:54

Simply add static in the declaration of these two methods and the compile time error will disappear!

By default in C# methods are instance methods, and they receive the implicit "self" argument. By making them static, no such argument is needed (nor available), and the method must then of course refrain from accessing any instance (non-static) objects or methods of the class.

More info on static methods
Provided the class and the method's access modifiers (public vs. private) are ok, a static method can then be called from anywhere without having to previously instantiate a instance of the class. In other words static methods are used with the following syntax:

    className.classMethod(arguments)
rather than
    someInstanceVariable.classMethod(arguments)

A classical example of static methods are found in the System.Math class, whereby we can call a bunch of these methods like

   Math.Sqrt(2)
   Math.Cos(Math.PI)

without ever instantiating a "Math" class (in fact I don't even know if such an instance is possible)

查看更多
登录 后发表回答