1

I have an event that is defined in a base class as:

public event EventHandler<HighestSeverityLevelChangedEventArgs> HighestSeverityLevelChanged;

The event argument is defined as:

   public sealed class HighestSeverityLevelChangedEventArgs : EventArgs
{
    /// <summary>
    /// Hold the highest severity level.
    /// </summary>
    public int HighestSeverityLevel { get; set; }

    /// <summary>
    /// Holds the previously high severity level.
    /// </summary>
    public int PreviousHighestSeverityLevel { get; set; }

    /// <summary>
    /// Initializes a new instance of the <see cref="HighestSeverityLevelChangedEventArgs" /> class.
    /// </summary>
    /// <param name="highestSeverityLevel">Input current high severity level</param>
    /// <param name="previousHighestSeverityLevel"></param>
    public HighestSeverityLevelChangedEventArgs(int highestSeverityLevel, int previousHighestSeverityLevel)
    {
        #region Preconditions

        if (highestSeverityLevel < 0)
        {
            throw new InvalidOperationException(nameof(highestSeverityLevel));
        }

        if (previousHighestSeverityLevel < 0)
        {
            throw new InvalidOperationException(nameof(previousHighestSeverityLevel));
        }

        #endregion

        HighestSeverityLevel = highestSeverityLevel;
        PreviousHighestSeverityLevel = previousHighestSeverityLevel;
    }
}

Now in a subclass, I have to raise the event, so I use the following line:

HighestSeverityLevelChanged?.Invoke(this, new HighestSeverityLevelChangedEventArgs(m_highestLevel, previousHighestLevel));

But this is not compiling, it is telling me that: The event can only appear in the left hand side of += or -+ excep when used within the base class.

I have this code in an inherited class, so is the syntax different here since I am trying to raise it in the inherited class. What am I doing wrong?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Ray
  • 4,679
  • 10
  • 46
  • 92
  • You have to provide a protected method to raise the event. – theMayer Apr 05 '18 at 17:40
  • The event can specifically only be fired from its exact class members. One of these members might be a protected method that fires the event and is visible to subclasses. – spender Apr 05 '18 at 17:41

0 Answers0