none
Error-An object reference is required for the non-static field, method, or property 'WindowsService1.Service1.eventLog1'

    Question

  • Why do I get this error for eventLog1 in the private static OnTimedEvent at line 77 below
    Error 1 An object reference is required for the non-static field, method, or property 'WindowsService1.Service1.eventLog1' 
    but I don't get the error for the other eventLog1 references in the catch statements of the protected methods OnStart and OnStop?

    1 using System;  
    2 using System.Collections.Generic;  
    3 using System.ComponentModel;  
    4 using System.Data;  
    5 using System.Diagnostics;  
    6 using System.Linq;  
    7 using System.ServiceProcess;  
    8 using System.Text;  
    9 using System.Timers;  
    10 using System.Configuration;  
    11  
    12  
    13 namespace WindowsService1  
    14 {  
    15     public partial class Service1 : ServiceBase  
    16     {  
    17         public static Timer smsTimer;    
    18    
    19         public Service1()  
    20         {  
    21             InitializeComponent();  
    22             try 
    23             {  
    24                 if (!System.Diagnostics.EventLog.SourceExists("MySource"))  
    25                 {  
    26                     System.Diagnostics.EventLog.CreateEventSource(  
    27                         "MySource""MyNewLog");  
    28                 }  
    29                 eventLog1.Source = "MySource";  
    30                 eventLog1.Log = "MyNewLog";  
    31             }  
    32             catch (Exception ex)  
    33             {  
    34                 eventLog1.WriteEntry("Exception thrown in Service1() = " + ex.ToString());     
    35             }  
    36         }  
    37  
    38         protected override void OnStart(string[] args)  
    39         {  
    40             //setup timer  
    41             //start timer  
    42             try 
    43             {  
    44                 eventLog1.WriteEntry("In OnStart");  
    45                 smsTimer.Interval = double.Parse(WindowsService1.Properties.Settings.Default.Interval);  
    46                 smsTimer.Enabled = true;  
    47             }  
    48             catch (Exception ex)  
    49             {  
    50                 eventLog1.WriteEntry("Exception thrown in OnStart() = " + ex.ToString());     
    51             }  
    52  
    53         }  
    54  
    55         protected override void OnStop()  
    56         {  
    57             try 
    58             {  
    59                 eventLog1.WriteEntry("In onStop.");  
    60             }  
    61             catch (Exception ex)  
    62             {  
    63                 eventLog1.WriteEntry("Exception thrown in OnStart() = " + ex.ToString());     
    64             }  
    65  
    66  
    67         }  
    68         private static void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)  
    69         {  
    70             try 
    71             {  
    72                 System.Console.WriteLine("myTimer event occurred");  
    73    
    74             }  
    75             catch (Exception ex)  
    76             {  
    77                 eventLog1.WriteEntry("Exception thrown in OnTimedEvent() = " + ex.ToString());     
    78             }  
    79         }  
    80  
    81     }  
    82 }  
    83  
    Thank you,
    -hazz
    Tuesday, November 04, 2008 10:23 PM

Answers

  • "eventLog1" is an instance field or property, and you're trying to access it from a static context.   You can't do that.  You'll either have to change the method OnTimedEvent to instance (by removing the static keyword from the method signature), or you'll have to change eventLog1 to a static field or property, by adding static to it's declaration.
    David Morton - http://blog.davemorton.net/
    • Marked as answer by hazz Tuesday, November 04, 2008 10:50 PM
    • Marked as answer by hazz Tuesday, November 04, 2008 10:50 PM
    Tuesday, November 04, 2008 10:29 PM
    Moderator

All replies

  • "eventLog1" is an instance field or property, and you're trying to access it from a static context.   You can't do that.  You'll either have to change the method OnTimedEvent to instance (by removing the static keyword from the method signature), or you'll have to change eventLog1 to a static field or property, by adding static to it's declaration.
    David Morton - http://blog.davemorton.net/
    • Marked as answer by hazz Tuesday, November 04, 2008 10:50 PM
    • Marked as answer by hazz Tuesday, November 04, 2008 10:50 PM
    Tuesday, November 04, 2008 10:29 PM
    Moderator
  • Thank you David.  When I make the change you suggest, adding the static keyword modifier as per; 

    partial class Service1

         private static System.Diagnostics.EventLog eventLog1;

    then I get the subsequent error for each place that this.eventLog1 is referenced within InitializeComponent in the same class;
    Error 1 Member 'WindowsService1.Service1.eventLog1' cannot be accessed with an instance reference; qualify it with a type name instead 


    1        private void InitializeComponent()  
    2         {  
    3             this.eventLog1 = new System.Diagnostics.EventLog();  
    4             ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();  
    5             //   
    6             // Service1  
    7             //   
    8             this.ServiceName = "Service1";  
    9             ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit(); 




    • Edited by hazz Tuesday, November 04, 2008 10:48 PM edit
    Tuesday, November 04, 2008 10:45 PM
  • I think I sort of get it. (sounds like a ringing endorsement of my confidence eh?)
    changing the eventlog to static means it is no longer an instance field/property and thus can't be referred to it that way.
    by simply removing the 'this' reference to from this.eventLog1 to just eventLog1, eventLog1 now can be referred as if it already exists, which it does in fact now, as a pre-instantiated field/property in its own right.
    ie. it doesn't and shouldn't be referred to through the class that contains it. 
    A better explanation please??? thx!
    Tuesday, November 04, 2008 10:54 PM
  • There's a difference between the instance and the type.  This instance is reflected in whether methods, properties, and fields are "static" or "instance".  By default, all methods, properties and fields declared are instance unless they're marked static, in which case they are methods, properties and fields of the type. 

    You can think of the type as a cookie-cutter, and the instance as a cookie.  Calling "new" creates a new cookie with the cookie-cutter.  If you have a static method, which is a method of the cookie cutter, calling an instance method, which would be an instance of the cookie, will require you to specify which cookie.  Likewise, trying to call a cookie cutter method on a specific cookie doesn't make much sense either.  So if you have a static method called "StaticMethod()", calling "this.StaticMethod()" is, in essence, saying "call the StaticMethod of this cookie" (as opposed to other cookies).  Since all the cookies (instances), were made from the same cookie cutter (type), that doesn't make much sense. 

    All the cookies can access all the methods of the cookie cutter, but in order for the cookie cutter to access methods of the cookie, they have to specify which cookie.   When calling a cookie-cutter method or property, it doesn't make much sense to call it on a "specific" cookie. 

    So, to bring this to techie terms, when .NET creates an instance of a type, the type, and all the related static methods, are created in memory just once.  If you change the value of a static property, then every instance of the type, that refers to this static property will reflect the change.  If a property is an instance property and you change it, the property value for the other instances will stay the same, and only the property attached to the specific instance will change. 

    Let's look at the following code:

    string value = "Bob";

    The "string" portion of this is the type, while "value" is the instance of the type.  Any methods or properties of "string" are static, while the methods and the properties of "value" are instance. 

    I hope this makes sense.  I hope I haven't made you too hungry.
    David Morton - http://blog.davemorton.net/
    Wednesday, November 05, 2008 4:36 PM
    Moderator
  • Thank you David. I'm digesting..  :-)
    Thursday, November 06, 2008 11:27 PM
  • hazz said:

    Thank you David. I'm digesting..  :-)

    Horrible pun.  Just horrible.  :)
    David Morton - http://blog.davemorton.net/
    Friday, November 07, 2008 2:33 PM
    Moderator
  • thank you david
    i have that problem
    your RECOMMENDATION  help me
    Monday, December 08, 2008 9:30 PM