Object Reference required?

An object reference is required for the non-static field, method, or p

Page 1 of 1

2 Replies - 6213 Views - Last Post: 16 September 2008 - 08:35 PM Rate Topic: -----

#1 chaosTechnician  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 27-May 08

Object Reference required?

Posted 16 September 2008 - 11:50 AM

I'm just starting a C# class and I'm not sure what this error means.

Quote

error CS0120: An object reference is required for the non-static field, method, or property 'HW2.Program.getEntry(string, out double)'

Excerpts of my code:
using System;
namespace HW2 {
  class Program {
    static int Main() {
      double  realPart1;
/* snip */
      // Get Real part of first number
      getEntry("Please enter the Real Part of the first complex number: ", out realPart1);  // Error is pointing to this line
/* snip */
      return 0;
    }//int Main
    
    public void getEntry(string prompt, out double output) {
      bool    loop;
      string  input;
      
      do
      {
        Console.Write(prompt);
        input = Console.ReadLine();
        loop = !double.TryParse(input, out output);
        if (loop)
        {
          Console.WriteLine("** Error with input.");
        }
      } while (loop);
    }//void getEntry
 /* snip */
  }//class Program
}//namespace HW2


I really have no idea what that error means to go about fixing it...

Is This A Good Question/Topic? 0
  • +

Replies To: Object Reference required?

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Object Reference required?

Posted 16 September 2008 - 12:27 PM

You're getting that because you're calling getEntry(), a non-static method, inside Main, which is a static method. Change getentry to

public static void getEntry(string prompt, out double output) {
      bool    loop;
      string  input;
      
      do
      {
        Console.Write(prompt);
        input = Console.ReadLine();
        loop = !double.TryParse(input, out output);
        if (loop)
        {
          Console.WriteLine("** Error with input.");
        }
      } while (loop);
}//void getEntry


Was This Post Helpful? 1
  • +
  • -

#3 chaosTechnician  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 27-May 08

Re: Object Reference required?

Posted 16 September 2008 - 08:35 PM

Fantastic! Thanks! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1