Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Virtual void not being overridden with override keyword c#

I am trying to make a class the user of my library can derive to then access a method for debugging the times of each action. Because my main debug method where most information is stored is static (and needs to be), I cannot use it to derive a class or add an overridable method in it. To combat this, I added the following code:

public static class Debug
{
    internal static void CallObjectEvent(string log)
    {
        new Call().CallEvent(new Log(log, Timer.GetTime()));
    }
}
internal class Call : IDebug
{
    internal void CallEvent(Log log)
    {
        base.Event(log);
    }
}
public class IDebug
{
    public virtual void Event(Log log) {Console.WriteLine("test");}
}

class Program : IDebug
{
    public override void Event(Log log)
    {
        Console.WriteLine(log.log);
    }
}

Every time, it outputs ‘test’ instead of the log message. I am wondering how to fix this or if there are any alternatives to do the same thing. Thanks :>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your Debug.CallObjectEvent() method explicitly instantiates a Call object and calls the overridden method in that class:

public static class Debug
{
    internal static void CallObjectEvent(string log)
    {
        new Call().CallEvent(new Log(log, Timer.GetTime()));
    }
}

The CallEvent() method in the Call class simply calls base.Event(), which resolves to IDebug.Event(). The Program.Event() override is never invoked because Program is not in the class hierarchy at the point of the call.

When you override a method or property, the override applies only to the class where it is defined (and all of its child classes, of course). Since Program isn’t a parent class of Call there’s no reason why its overrides would ever be referenced.

From what you’ve written it looks like you’re trying to set up a system for handling different log outputs depending on the program’s requirements. You need a way to register the appropriate log writer for your program. Something like:

public interface ILogWriter
{
    void Event(Log item);
}

private class DefaultLogWriter : ILogWriter
{
    public void Event(Log item)
    {
        Console.WriteLine($"[test] {item.Time} {item.Message}");
    }
}

internal static class Debug
{
    private static ILogWriter _writer = null;

    public static ILogWriter Writer 
    { 
        get
        {
            if (_writer == null)
                _writer = new DefaultLogWriter();
            return _writer;
        }
        set => _writer = value;
    }

    internal static void CallObjectEvent(string log)
    {
        Writer.Event(new Log(log, Timer.GetTime()));
    }
}

class Program
{
    private class MyLogWriter : ILogWriter
    {
        public void Event(Log item)
        {
            Console.WriteLine($"[MyLogWriter] {item.Time} {item.Message}");
        }
    }

    static void Main()
    {
        Debug.Writer = new MyLogWriter();

        Debug.CallObjectEvent("Test message.");
    }       
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading