2

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 :)

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Tim Lagerburg
  • 39
  • 1
  • 8
  • 4
    simply add `static` modifier to your isPalindroom method – Steve B Sep 04 '12 at 14:17
  • 1
    @SteveB - post this as an answer - it IS the answer and you'll get we'll all up-vote you for it :) – n8wrl Sep 04 '12 at 14:18
  • 2
    You can't call non-static methods from static methods. :) http://stackoverflow.com/questions/498400/an-object-reference-is-required-for-the-nonstatic-field-method-or-property-wi – Thomas Clayson Sep 04 '12 at 14:18
  • 1
    and please remove the `== true`. – Henrik Sep 04 '12 at 14:19
  • 1
    Good luck as you learn C#!!! Not the subject of your question (Someone beat me to that), but a few tips: There is an easier way to reverse the string with less code. http://www.dotnetperls.com/reverse-string Also, in your if statement, you can eliminate "== true" and just say `if (isPalindroom(str))` – David Sep 04 '12 at 14:21
  • 2
    and simplify the isPalindrom method : `return str == new string(str.ToCharArray().Reverse().ToArray());` – Raphaël Althaus Sep 04 '12 at 14:22
  • 2
    `== true` is useless regarding the compilation (and I'm confident the compiler optimize this), but some people (not me) think this is more readable. I would say that we should let everyone decide whether or not it's useful. – Steve B Sep 04 '12 at 14:23

8 Answers8

5

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.

Steve B
  • 36,818
  • 21
  • 101
  • 174
3

Make the function static:

static bool isPalindrome(String str)
{
}

The Main() method is static (by requirement) and thus it can only call static members.

And because your bool isPalindrome() is a 'pure' function, ie it only requires data from its parameters, it can be static.

Small nitpick: always use PascalCasing for method names, IsPalindrome

H H
  • 263,252
  • 30
  • 330
  • 514
1

Change bool isPalindroom(String str)

to static bool isPalindroom(String str)

NominSim
  • 8,447
  • 3
  • 28
  • 38
1

The declaration of "isPalindrome" needs to be modified to "static bool isPalindrome(String str)", or you need to put it in as a method of a class instance you can instantiate in Main and call from there.

David W
  • 10,062
  • 34
  • 60
1

Main is static.. your function

  bool isPalindroom(String str) 

should be

static  bool isPalindroom(String str) 
BugFinder
  • 17,474
  • 4
  • 36
  • 51
1

Make it;

static bool isPalindroom(String str)
{
 ....
}

The Main routine is static so you are not in an instance of the program class and cannot call an instance method. Making the isPalindroom a static routine will fix this.

For more on static methods and classes see;

http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx

IvoTops
  • 3,463
  • 17
  • 18
1

As others already mentioned, you should make isPalindroom() method static.

Here a quick note about the way you're reversing the string:

Strings in C# are immutable, it means every time you're concatenating a new string (or character) to an existing one, it makes a whole new object. So to make the reverse routine more efficient, you may want to use StringBuilder and StringBuilder.Append().

Aside from that, instead of:

string reversedString = "";
for (int i = str.Length - 1; i >= 0; i--)
{
    reversedString += str[i];
}

you can simply type:

string reversedString = new string(str.Reverse().ToArray())
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
fardjad
  • 20,031
  • 6
  • 53
  • 68
1

here you go, a better culture/case/normalization insensitive version

using System.Globalization;

bool IsPalindrome(string value, StringComparer comparer = null)
{
    if (s == null)
    {
        throw new ArgumentNullException("value");
    }

    if (comparer == null)
    {
        comparer = StringComparer.CurrentCultureIgnoreCase;
    }

    var elements = new List<string>();
    var m = StringInfo.GetTextElementEnumerator(value);
    while (m.MoveNext())
    {
        elements.Add(m.GetTextElement());
    }

    var i = 0;
    var j = elements.Count - 1;
    var limit = elements.Count / 2;
    for(; i <= limit; i++, j--)
    {
        if (!comparer.Equals(elements[i], elements[j]))
        {
            return false;
        }
    }

    return true;
}
Jodrell
  • 34,946
  • 5
  • 87
  • 124