I'm quite new to C# and I was making a small application to check if the input of the console is a palindrome. I came pretty far by myself, but I got stuck with an error.
Code:
class Program
{
static void Main(string[] args)
{
string str;
Console.WriteLine("Voer uw woord in:");
str = Console.ReadLine();
if (isPalindroom(str) == true)
{
Console.WriteLine(str + " is een palindroom");
}
else
{
Console.WriteLine(str + " is geen palindroom");
}
}
bool isPalindroom(String str)
{
string reversedString = "";
for (int i = str.Length - 1; i >= 0; i--)
{
reversedString += str[i];
}
if (reversedString == str)
{
return true;
}
else
{
return false;
}
}
}
I get this error:
Error 1 An object reference is required for the non-static field, method, or property 'ConsoleApplication2.Program.isPalindroom(string)' snap 17 17 ConsoleApplication2
Which is at:
if (isPalindroom(str) == true)
If you could help me a bit, I'd be very pleased :)
Simply add static
modifier to your isPalindroom
method.
If you don't, isPalindroom
will be an "instance" method, that can be called on a Program
instance.
To be simple, as you don't have a variable of kind Program (main method itself is static), you can't call a non-static method.
A static method can be called either on the type itself (Program.isPalydroom(xxx)
or from any other method in the class.