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:
BaseClass.Write()
methodBaseClass.consoleWriter
member.BaseClass.consoleWriter
variableThen
NewClass.Write()
NewClass.consoleWriter
object. BaseClass
, but the method is inherited throughNewClass
) using the NewClass.consoleWriter
variableI 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;
}
}
}
...
The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.
For example: int *a=new int; In the above example, the new operator allocates sufficient memory to hold the object of datatype int and returns a pointer to its starting point. The pointer variable holds the address of memory space allocated.
*new int means "allocate memory for an int , resulting in a pointer to that memory, then dereference the pointer, yielding the (uninitialized) int itself".
The new operator is used in Java to create new objects. It can also be used to create an array object. Declaration − A variable declaration with a variable name with an object type. Instantiation − The 'new' keyword is used to create the object.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With