Home > Backend Development > C++ > How Can I Properly Raise Inherited Events in C# Derived Classes?

How Can I Properly Raise Inherited Events in C# Derived Classes?

Patricia Arquette
Release: 2024-12-24 11:48:15
Original
224 people have browsed it

How Can I Properly Raise Inherited Events in C# Derived Classes?

Raising Events Inherited from a Base Class in C#

In C#, it is common practice to inherit events from a base class to facilitate event handling in derived classes. However, raising such inherited events requires a specific approach to avoid compiler errors.

Consider a scenario where a base class defines the following events:

public class BaseClass
{
    public event EventHandler Loading;
    public event EventHandler Finished;
}
Copy after login

In a derived class, attempting to raise the Loading event using:

this.Loading(this, new EventHandler());
Copy after login

results in the error:

The event 'BaseClass.Loading' can only appear on the left hand side of += or -= (BaseClass')
Copy after login

This error occurs because events, unlike other class members, cannot be invoked directly by the derived class. Instead, inherited events must be raised by invoking specific methods defined in the base class. To achieve this, the following steps are necessary:

  1. Create Protected Event-Raising Methods in the Base Class:
    Define protected methods in the base class that are responsible for raising the events. For example:
public class BaseClass
{
    public event EventHandler Loading;
    public event EventHandler Finished;

    protected virtual void OnLoading(EventArgs e)
    {
        EventHandler handler = Loading;
        if( handler != null )
        {
            handler(this, e);
        }
    }

    protected virtual void OnFinished(EventArgs e)
    {
        EventHandler handler = Finished;
        if( handler != null )
        {
            handler(this, e);
        }
    }
}
Copy after login
  1. Invoke Event-Raising Methods in Derived Classes:
    In derived classes, instead of directly invoking the events, call the corresponding event-raising methods defined in the base class. For instance:
public class DerivedClass : BaseClass
{
    public void DoSomething()
    {
        // Raise Loading event
        OnLoading(EventArgs.Empty);

        // Raise Finished event
        OnFinished(EventArgs.Empty);
    }
}
Copy after login

By following this approach, inherited events can be safely and efficiently raised in derived classes in C#.

The above is the detailed content of How Can I Properly Raise Inherited Events in C# Derived Classes?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template