Declaring an event handler private static

I could use some help with a question regarding object oriented programming. I’ve written an error handler class that uses a callback to process the error after a certain number of errors occur. Everything works fine in my test program where I instantiate the error handler like this

   public class Program
    {
        public static void Main()
        {
            Debug.Print("Program Started");

            TimeSpan error0TimeLimit = new TimeSpan(0, 0, 3);
            StringBuilder error0Label = new StringBuilder("error0 label");
            ErrorHandler error0 = new ErrorHandler(3, TimeSpan.Zero, error0Label, ErrorType.critical);
            error0.criticalErrorHandler += new CriticalErrorHandler(error0Handler);

The idea is to instantiate error handlers wherever I need them generally inside other classes. Seems like I should be instantiating all of the error handler properties and methods as private static but I don’t seem to be able to figure out how to declare the callback that way.

This works

       private static TimeSpan overHumidityErrorTS = new TimeSpan(1, 0, 0);
        private static StringBuilder overHumidityLabel = new StringBuilder("Over Humidity Error");
        private static ErrorHandler overHumidityError = new ErrorHandler(3, overHumidityErrorTS, overHumidityLabel, ErrorHandler.ErrorType.critical);

        private static double GetHumidity()
        {
            overHumidityError.criticalErrorHandler += new ErrorHandler.CriticalErrorHandler(ErrorHandler.setRecoveryMode);

but what I think I should be doing is this

        private static TimeSpan overHumidityErrorTS = new TimeSpan(1, 0, 0);
        private static StringBuilder overHumidityLabel = new StringBuilder("Over Humidity Error");
        private static ErrorHandler overHumidityError = new ErrorHandler(3, overHumidityErrorTS, overHumidityLabel, ErrorHandler.ErrorType.critical);
        private static ErrorHandler.CriticalErrorHandler overHumidityError.criticalErrorHandler += new ErrorHandler.CriticalErrorHandler(ErrorHandler.setRecoveryMode);

Which give these errors

Error 1 ; expected
Error 2 Invalid token ‘+=’ in class, struct, or interface member declaration
Error 3 Invalid token ‘(’ in class, struct, or interface member declaration
Error 4 Invalid token ‘)’ in class, struct, or interface member declaration
Error 5 The type or namespace name ‘criticalErrorHandler’ could not be found (are you missing a using directive or an assembly reference?)
Error 6 ‘CPF.ErrorHandler.setRecoveryMode(object, CPF.ErrorHandler.ErrorType)’ is a ‘method’ but is used like a ‘type’

My error handler class looks like this

  class ErrorHandler
    {
        public enum ErrorType
        {
            warning = 0,
            nonCritical,
            critical
        }

        public delegate void CriticalErrorHandler(object sender, ErrorType e);
        public event CriticalErrorHandler criticalErrorHandler;

        private static StructQueue.qStruct qS = new StructQueue.qStruct();

        private int errorCount = 0;
        private int maxErrors = 1;
        private TimeSpan timeLimit = new TimeSpan(0, 0, 0);
        private StringBuilder label = new StringBuilder(64);
        private DateTime startTime;
        public bool goneCritical = false;
        public ErrorType errorType;

        public static StringBuilder sbTemp = new StringBuilder(128);

        public ErrorHandler(int maxErrors, TimeSpan timeLimit, StringBuilder label, ErrorType errorType)
        {
            this.maxErrors = maxErrors;
            this.timeLimit = new TimeSpan(timeLimit.Hours, timeLimit.Minutes, timeLimit.Seconds);
            this.label.Clear();
            this.label.Append(label);
            this.errorType = errorType;

            qS.byteArray = new byte[StructQueue.byteArrayWidth];
        }

        public void logError()
        {

As always, any help will be greatly appreciated.

Thanks - Gene

Hi Gene,

this


can never work.
You define a field with no value, means null will be assigned, and then you try to attach an Event (to null!?)

you should attach the Event handler in the static constrcutor like this:

```cs
public static class ThisIsYouClassName
{
  private static TimeSpan overHumidityErrorTS = new TimeSpan(1, 0, 0);
  private static StringBuilder overHumidityLabel = new StringBuilder("Over Humidity Error");
  private static ErrorHandler overHumidityError = new ErrorHandler(3, overHumidityErrorTS, overHumidityLabel, ErrorHandler.ErrorType.critical);

  static ThisIsYouClassName()
  {
      overHumidityError.criticalErrorHandler += new ErrorHandler.CriticalErrorHandler(ErrorHandler.setRecoveryMode);
  }
}

The static constructor is called once the type is used for the first time.
Before that all static fields are initialized.
Also:
If a class has no static constructor, then not all static fields are initialized on the first usage of the type.
The fields are only initialized if the field is used then!

Edit:
you can also shorten your Code:
this


is the same as

```cs]overHumidityError.criticalErrorHandler += ErrorHandler.setRecoveryMode;[/code

The Compiler knows waht to do :D
1 Like

@ Reinhard Ostermeier - Thanks for an excellent tutorial. I get it now.