Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have code that works in a form app. I transferred the code to a console app in order to do multiple analyses and have made all the corrections for inputting variables that came from the form controls. All the code works in the console app, except that which is associated with an event handler. In the code below, I get the following 2 errors in the locations shown:
Error 1: CS0120 An object reference is required for the non-static field, method, or property 'Program.OnIterationEvent'
Error 2: CS0026 Keyword 'this' is not valid in a static property, static method, or static initializer

I haven't been able to find anything using Google searches that helps. Why would the event handler work in a form app but not in a console app? Any help is much appreciated.

What I have tried:

I have the following separate class:
Class IterationEventArgs.cs
// normal "using" statements
C#
namespace Limits
{
    class IterationEventArgs : EventArgs
    {
        public int IterationNumber { get; set; }

        public IterationEventArgs(int aNumber)
        {
            IterationNumber = aNumber;
        }
    }
}


The main program includes the following:

// normal "using" statements plus:
// with or without 'public delegate void EventHandler();'

C#
namespace Limits
{
   Class Program
   {
      public event EventHandler<IterationEventArgs> OnRaiseIterationEvent;
      static void Main(string[] args)
      {
         // code
         void RaiseIterationEvent(int iterationNumber)
         {
            if (OnRaiseIterationEvent != null) // Error 1
                OnRaiseIterationEvent(this, new iterationEventArgs(iterationNumber)); //Error 1, 2
            else
                return;
         }
      }
   }
}
I have a method within the Main method that calls the RaiseIterationEvent method
within a for loop using:

RaiseIterationEvent(iterationNumber1);
Posted
Updated 8-Apr-19 8:18am

Notice that the main method is marked as static

C#
static void Main(string[] args)


The initial problem is that the EventHandler you are attempting to use is non-static.
C#
public event EventHandler<IterationEventArgs> OnRaiseIterationEvent;


The static method Main(), cannot access the non-static member OnRaiseIterationEvent.

You may be able to fix this simply by making the item static.
C#
static event EventHandler<IterationEventArgs> OnRaiseIterationEvent;


But, there may be other implications to that.

Your second error is caused because your static method has no concept of the this variable. this is only available on instance objects (non-static).
 
Share this answer
 
v3
Comments
BillWoodruff 9-Apr-19 4:34am    
+5
The simplest way is to remember that a Form is a Class, albeit with extra properties and methods.

So take your Form based code and put it all in a class, then create an instance of that class in your Main method and use it all in a similar way - but passing in the parameters it needs instead of fetching them from textboxes.
There are a lot less changes involved that trying to force the code into a static context - and since you wrote the Forms based code you already understand how it works so it should be pretty trivial, yes?
 
Share this answer
 
If you're not handling "sender", which is usually the first parameter in an event handler's signature, then just pass "null" (instead of what you're trying).

You can pass anything you want; you're not restricted to the "real" "sender".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900