-1

I'm trying to implement an event for logging:

In the following example, all works well:

public class BaseElement
{
    internal delegate void ReportWriter(string report);
    internal event ReportWriter Log;

    internal void Click()
    {
            WebElement.Click();
            Log?.Invoke("The " + Name + " is clicked");
    }
}

But when I try to use Log in the inherited class, the methods on the Log aren't allowed to be used.

public class Button : BaseElement
{
    //....

    internal void Clear()
    {
            WebElement.Clear();
            Log.//not allowed any methods
    }
}

I do not want to initialize the delegate and Log an event each time in each class - how do I use one event for all cases?
enter image description here

Luke Machowski
  • 3,983
  • 2
  • 31
  • 28
Valentyn Hruzytskyi
  • 1,772
  • 5
  • 27
  • 59

1 Answers1

1

Add the following method to your BaseElement class:

internal void LogInvoke(string report)
{
    Log?.Invoke(report);
}

Then you can invoke it from the inherited Button class by calling the LogInvoke method.

public class Button : BaseElement
{
    //....

    internal void Clear()
    {
            WebElement.Clear();
            LogInvoke("clear");
    }
}
danworley
  • 28
  • 3