• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

An object reference is required for the nonstatic field, method

I am a novice at C# and I am getting an error when trying to call a subroutine:
~\Class1.cs(37): An object reference is required for the nonstatic field, method, or property 'VinExplosion.Class1.updateDB()'

code:

            static void Main(string[] args)
            {

                  // QUERY DATABASE AND CREATE CSV FILE
                  // queryDB();                             ~~~~ REMOVE TO EXECUTE

                  // EXECUTE APPLICATION

                  // PASRE DATA AND UPDATE DATABASE
                  updateDB();                          ~~~~~~~~~~~~~~~~~~~~ ERROR ON THIS LINE

                  
            }


            void updateDB()
            {
                  // READ CSV FILE AND UPDATE DATABASE
                  FileStream file = new FileStream("C:\\TEMP\\OUTPUT.csv", FileMode.Open, FileAccess.Read);
                  StreamReader sr = new StreamReader(file);
                  string s = sr.ReadToEnd();
                  Console.Write(s);
                  sr.Close();
                  file.Close();
            }

I would appreciate any advice, I am sure that it is syntax or something simple..

Thank You
CJSantora
0
CJSantora
Asked:
CJSantora
  • 2
1 Solution
 
AlexFMCommented:
void updateDB()

replace with:

static void updateDB()
0
 
AlexFMCommented:
Static function is function which doesn't require class instance. Non-static function requires class instance for call. Since Main function is static and updateDB is called from it without class instance, updateDB function must be static as well.
0
 
YurichCommented:
and just to complete the above, you can either have it static or add an instance of you class:
So, if you still have your default name Class1, it will be:

[STAThread]
static void Main(string[] args)
{
      Class1 c1 = new Class1();
      c1.Test();
}

and everything will compil and run just fine.

regards,
yurich
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now