10
votes

I have a public static class in which I would like to have a ToString() method.

I have defined it as public static string ToString(), but get the following warning:

'Class.ToString()' hides inherited member 'object.ToString()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

If I add the override keyword I get this error instead:

A static member 'Class.ToString()' cannot be marked as override, virtual, or abstract

How do I get rid of that warning and let my static class have the ToString() method.

Thank you,
Keith

3
Can I ask why you would want to do this? (Just curious)StingyJack
I have a ErrorLog that is a static class, through out my code I can add an ErrorLogItem to the ErrorLog. The ErrorLog can save itself to the database by saving all ErrorLogItems. If the database is not accessible (major error), I can convert the entire log to a string and save it to disk.Keith Sirmons
Two things about this discussion. First, you should have mentioned that you meant .NET although it became clear from the spelling and the subsequent discussion.orcmid
Secondly, reusing ToString for this is weird, as are all of the inter-method dependencies on a static class. Consider something like DumpLogToString(). Better yet, figure out an object-oriented way to handle this using a log-access instance rather than a static class.orcmid
Ok, so reading some of these comments and doing a little research on the internet, It looks like I may have misused this static class. Here is a reference that I am looking at: dotnet.dzone.com/news/c-singleton-pattern-vs-static-Keith Sirmons

3 Answers

14
votes

Yes, using the "new" modifier will effectively silence the compiler warning but you are explicitly hiding an instance method with a static method. (This is different than overriding the method.) Typically you don't want to hide an instance method except with very good reasons and you really shouldn't hide it with a static method as that really changes the behavior semantics of the call. Every object in .NET has an instance method named ToString() that has specific behavior that developers expect; by hiding that behavior with a new static method you are changing that expectation which can lead to a lot of confusion.

What are you "to stringing"? Static classes typically don't hold internal state so there really shouldn't be any internal data to provide as the logical output of a ToString() call. You may want to rethink your class design or provide a different method name that more clearly indicates the purpose of the method without hiding the instance ToString().

3
votes

In a static class you cannot override ToString. .ToString is an instance method and by definition a static class can only have static members.

Also why would you want to override .ToString()? There is no way to get an instance of the class and hence no way to call the function.

Note: Using the new syntax will not override .ToString. It will create a new member that is completely unrelated to the Object.ToString() method.

2
votes

Ok, so in asking the question, I found an answer:

The new Modifier:

http://msdn.microsoft.com/en-us/library/51y09td4(VS.71).aspx#vclrfnew_newmodifier

here is the method now:

public new static string ToString()

Thank you, Keith