Link to home
Start Free TrialLog in
Avatar of CJSantora
CJSantora

asked on

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
Avatar of AlexFM
AlexFM

void updateDB()

replace with:

static void updateDB()
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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