0

Possible Duplicate:
“An object reference is required for the non-static field, method, or property”

I'm trying to get to grips with programming, and have just started learning C#, so first off, apologies for probably asking a really basic question which may have been answered hundreds of times. However, I can't seem to understand the answers and relate the answers out there to what I am trying to do.

I keep getting "An object reference is required for the non-static field, method or property 'FirstConsoleApplication.Program.function(string)'"

Can anyone point out where I'm losing this? I'm using visual studio 2008.

namespace FirstConsoleApplication
{
   class Program
   {   
       static void Main(string[] args)
       {
          Console.WriteLine("Type in an integer vale");
          string num;
          num = Console.ReadLine();
          string result1 = function1(num);
          Console.WriteLine(result1);
          Console.ReadLine();
       }

       string function1(string x)
       {
          Int32 isnumber = 0;
          bool canConvert = Int32.TryParse(x, out isnumber);
          string returnValue;
          if (canConvert == true)
          {
             int val3 = Int32.Parse(x);

             switch (val3)
             {
                case 50:
                    returnValue = "yep its 50";
                    break;
                case 51:
                    returnValue = "hmmm.... its 51... what are you gonna do about that??";
                    break;
                case 52:
                    returnValue = "lets not get sloppy now...";
                    break;
                default:
                    returnValue = "nope, its definately something else";
                    break;
            };

        }
        else
        {
            returnValue = "Thats not a number";
        };
        return returnValue;
    }
}

}

Community
  • 1
  • 1
Nav
  • 95
  • 10
  • Do you see off to the right, where it says "Related"? And there are a bunch of people with the same question who already got answers that could help? Shame on anyone adding answers to this question. – Ben Voigt Jan 28 '13 at 22:57
  • `Console.WriteLine("Type in an integer vale");` should be, `Console.WriteLine("Type in an integer value");`... just sayin'! – Brian Jan 28 '13 at 23:01

4 Answers4

1

You need to declare the function as static:

static string function1(string x)
{
    ...
}

When you create a class like Program in your example, you can only call non-static methods if you first declare an instance of that class. Static methods do not require an instance of your Program class. In your example you don't have any properties or class variables to worry about, so it makes sense to declare your function as static.

JoshVarty
  • 9,066
  • 4
  • 52
  • 80
0

You have to put static keyword in front of function1 like

static string function1(string x)
Nick
  • 4,192
  • 1
  • 19
  • 30
0

Your Main() function is static (one per class) while your function1() function is not (one per instance). Adding "static" before "string function1(string x)" should fix this for you.

0

Complete code

namespace FirstConsoleApplication
{
class Program
    {   
        static void Main(string[] args)
        {

            Console.WriteLine("Type in an integer vale");
            string num;
            num = Console.ReadLine();
            string result1 = function1(num);
            Console.WriteLine(result1);
            Console.ReadLine();
        }

        static string function1(string x)
        {
            Int32 isnumber = 0;
            bool canConvert = Int32.TryParse(x, out isnumber);
            string returnValue;
            if (canConvert == true)
            {
                int val3 = Int32.Parse(x);

                switch (val3)
                {
                    case 50:
                        returnValue = "yep its 50";
                        break;
                    case 51:
                        returnValue = "hmmm.... its 51... what are you gonna do about that??";
                        break;
                    case 52:
                        returnValue = "lets not get sloppy now...";
                        break;
                    default:
                        returnValue = "nope, its definately something else";
                        break;
                };

            }
            else
            {
                returnValue = "Thats not a number";
            };
            return returnValue;
        }
    }
}
Nick
  • 4,192
  • 1
  • 19
  • 30
  • 1
    You should probably leave this as an edit in your previous answer. – JoshVarty Jan 28 '13 at 23:02
  • Sure but guy said that he's beginner so I gave him multiple solutions – Nick Jan 28 '13 at 23:04
  • 1
    Thanks! wow, wasn't expecting answers this quick! I haven't quick got my head around the use of 'static' so I think I'll need to read up on that. – Nav Jan 28 '13 at 23:11