views:

150

answers:

4

I am confused as to why the new operator isn't working as I expected it to.

Note: All classes below are defined in the same namespace, and in the same file.

This class allows you to prefix any content written to the console with some provided text.

public class ConsoleWriter
{
    private string prefix;

    public ConsoleWriter(string prefix)
    {
        this.prefix = prefix;
    }

    public void Write(string text)
    {
        Console.WriteLine(String.Concat(prefix,text));
    }
}

Here is a base class:

public class BaseClass
{
    protected static ConsoleWriter consoleWriter = new ConsoleWriter("");

    public static void Write(string text)
    {
        consoleWriter.Write(text);
    }
}

Here is an implemented class:

public class NewClass : BaseClass
{
    protected new static ConsoleWriter consoleWriter = new ConsoleWriter("> ");
}

Now here's the code to execute this:

class Program
{
    static void Main(string[] args)
    {
        BaseClass.Write("Hello World!");
        NewClass.Write("Hello World!");

        Console.Read();
    }
}

So I would expect the output to be

Hello World!
> Hello World!

But the output is

Hello World!
Hello World!

I do not understand why this is happening. Here is my thought process as to what is happening:

  1. The CLR calls the BaseClass.Write() method
  2. The CLR initialises the BaseClass.consoleWriter member.
  3. The method is called and executed with the BaseClass.consoleWriter variable

Then

  1. The CLR calls the NewClass.Write()
  2. The CLR initialises the NewClass.consoleWriter object.
  3. The CLR sees that the implementation lies in BaseClass, but the method is inherited through
  4. The CLR executes the method locally (in NewClass) using the NewClass.consoleWriter variable

I thought this is how the inheritance structure works?

Please can someone help me understand why this is not working?

--

Update :

This scenario would work as follows (how I've implemented it)

public class LogBase
{
   protected static TraceSource logger = new TraceSource("");

   public static void Error (string text) { logger.WriteError(text); }
   public static void Info (string text) { logger.WriteInformation(text); }
   public static void Warning (string text) { logger.WriteWarning(text); }
   public static void Verbose (string text) { logger.WriteVerbose(text); }
}

// DataAccess logging
public class DALog : LogBase
{
    protected new static TraceSource logger = new TraceSource("DataAccess");
}

// BusinessObjects logging
public class BOLog : LogBase
{
    protected new static TraceSource logger = new TraceSource("Objects");
}

// BusinessLogic logging
public class BLLog : LogBase
{
    protected new static TraceSource logger = new TraceSource("Logic");
}

// WebUI logging
public class WebUILog : LogBase
{
    protected new static TraceSource logger = new TraceSource("WebUI");
}

The reason being I don't have to duplicate the code for every single class.

--

Update (after solution chosen):

So in order to get around this problem, instead of using base classes, I defined the functionality one. Then created new classes, which held Singleton instances for each layer:

public sealed class LogBase
{
   private TraceSource logger = null;

   public static LogBase GetLogger(string loggerName) 
   {
       return new LogBase(loggerName);
   }

   private LogBase(string loggerName) { logger = new TraceSource(loggerName); }

   public void Error (string text) { logger.WriteError(text); }
   public void Info (string text) { logger.WriteInformation(text); }
   public void Warning (string text) { logger.WriteWarning(text); }
   public void Verbose (string text) { logger.WriteVerbose(text); }
}

// DataAccess logging - no base class
public class DALog 
{
    private static LogBase logger;

    public static LogBase Instance
    { 
         get 
         { 
              if (logger==null) { logger = new TraceSource("DataAccess"); }
              return logger;
         }
    }
}

...
+7  A: 

The new operator does not actually override in the sense of polymorphism. It just adds another method which "happens" to have the same signature and yet is treated as a separate method. Only if you execute that method explicitly on the subclass, the "new" implementation will be used.

In your case, you have implemented Write only on the base class. In there, you have the line that calls consoleWriter. That line refers to the base class and thus uses the original implementation. It doesn't even know about the additional implementation in the subclass.

Review your code and regard it as it would be:

public class BaseClass
{
    protected static ConsoleWriter consoleWriter = new ConsoleWriter("");

    public static void Write(string text)
    {
        consoleWriter.Write(text);
    }
}


public class NewClass : BaseClass
{
    protected static ConsoleWriter anotherConsoleWriter = new ConsoleWriter(">");
}

Your BaseClass.Write wouldn't ever consider to call anotherConsoleWriter instead of consoleWriter.

If you want your example to work, you either need to add a new implementation for Write:

public class NewClass : BaseClass
{
    protected new static ConsoleWriter consoleWriter = new ConsoleWriter(">");

    public static new void Write(string text)
    {
        consoleWriter.Write(text);
    }
}

... or, what you most probably originally wanted, rather introduce real overriding in the sense of polymorphism by using override instead of new. However, your base class needs to support that by declaring consoleWriter as virtual. Further (as you mentioned in your comment) this only works if you don't make these methods static.

You could apply the Singleton pattern instead if you still want static access to your loggers, or have a look to log4net which already solved all these problems for you.

chiccodoro
I can't declare static methods as virtual. Maybe I should return an instance of the class and then call non-static methods to get around this issue?
Dominic Zukiewicz
Yes, do that. You can make it a singleton and still provide a static way to access it like: LogBase.GetLogger("DataAccess"). And then you're almost at how it looks like in log4net, which you might want to have a look to.
chiccodoro
+1  A: 

Your missing a few crucial points.. Static makes it a class level field. Protected means it's available from derived classes. New means you are hiding the base member.

Do you need these classes to be static? If not you can make the Write method virtual and override it in the derived class; base.Write(">" + text);

simendsjo
I want it to be static because I can define all my methods in a base class, which in turn calls a static member (in the actual implementation it is a TraceSource).Therefore the only thing I have to change with regards to my inherited base classes is the overridden implementation. I will update the question.
Dominic Zukiewicz
+1  A: 

You do know that inheritance and overriding works for (virtual) instance methods, not static ones, right?

Your override is protected and therefore not visible outside your NewClass and descendants. Therefore, the behaviour is by spec. new only allows you to create a new function which can be referred to by the same identifier in its context: your NewClass.Write method really has nothing to do with your BaseClass.Write.

Pontus Gagge
Thanks Pontus. All the keywords were chosen for a specific purpose, however I do now understand they were incorrect. Here's why:'protected' was because I did only want sub-classes to see the member, in order to override it. 'new' as I wanted the method to use the overridden version. And 'static' as the member was to be used alongside static methods (for convenience really, instead of me having to 'new' it up every time).But as we have seen described here, static members and methods are NOT inherited through.
Dominic Zukiewicz
+3  A: 

The original question has been answered by others: the behavior the OP expected is what you get from overriding a method, not what you get from using new.

In the OP's responses he's concerned that you can't override static methods. That's true, but here's a blog from msdn that both explains why and discusses ways of dealing with it: http://blogs.msdn.com/b/kirillosenkov/archive/2008/02/06/how-to-override-static-methods.aspx

Tim Goodman