473,396 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How can I force a class to override the ToString() method?

How can I force anyone who subclasses my class to override the ToString()
method?

Andreas :-)

Nov 17 '05 #1
7 4516
Andreas,

You can do it like this:

public override string ToString()
{
// Do something.
return "This is a different string than what would be normally
returned";
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"AWHK" <aw**@newsgroup.nospam> wrote in message
news:uW**************@tk2msftngp13.phx.gbl...
How can I force anyone who subclasses my class to override the ToString()
method?

Andreas :-)

Nov 17 '05 #2
throw a NotImplementedException in your ToString() method to indicate that
ToString() should be overridden.

"AWHK" <aw**@newsgroup.nospam> schrieb im Newsbeitrag
news:uW**************@tk2msftngp13.phx.gbl...
How can I force anyone who subclasses my class to override the ToString()
method?

Andreas :-)

Nov 17 '05 #3
In message <uW**************@tk2msftngp13.phx.gbl>, AWHK
<aw**@newsgroup.nospam> writes
How can I force anyone who subclasses my class to override the ToString()
method?


Surprisingly enough, this is allowed:

abstract class Foo
{
public abstract override string ToString();
}
class Bar:Foo
{
public override string ToString()
{
return "Override";
}

}
--
Steve Walker
Nov 17 '05 #4
Thanks Folks!

That worked well Steve.

Any idea how to force any subclasses to override operators?

like ie.
public static bool operator !=(MyType lhs, MyType rhs)

Guess i can throw a NotImplementedException, as recommended by Cody, in the
case of operators?

Andreas :-)
"Steve Walker" <st***@otolith.demon.co.uk> wrote in message
news:kO**************@otolith.demon.co.uk...
In message <uW**************@tk2msftngp13.phx.gbl>, AWHK
<aw**@newsgroup.nospam> writes
How can I force anyone who subclasses my class to override the ToString()
method?


Surprisingly enough, this is allowed:

abstract class Foo
{
public abstract override string ToString();
}
class Bar:Foo
{
public override string ToString()
{
return "Override";
}

}
--
Steve Walker

Nov 17 '05 #5
Yes, you can throw an exception in the base operator.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #6
> Surprisingly enough, this is allowed:

abstract class Foo
{
public abstract override string ToString();
}
class Bar:Foo
{
public override string ToString()
{
return "Override";
}

}


Very fascinating, I also didn't know that. But the principle is simple, it
simple sets the entry of the ToString() method in the vtable of the class
Foo to null.
Nov 17 '05 #7
In message <#L**************@TK2MSFTNGP10.phx.gbl>, AWHK
<aw**@newsgroup.nospam> writes
Any idea how to force any subclasses to override operators?


Again assuming the base class is abstract you could define the operators
in the base class and then delegate the calculation to abstract template
methods which the derived classes must define.

In the case of the equality operator you could define the == and !=
operators in the base class and force Equals() and GetHashCode() to be
overridden using an abstract override in the same way as ToString().

--
Steve Walker
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ovid | last post by:
Hi, I'm trying to determine the cleanest way to override class data in a subclass. class Universe { public String name; private static double PI = 3.1415; Universe(String name) {
9
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; ...
8
by: Sunny | last post by:
Hi all, The code as follow: using System; class A { public virtual void F() {
5
by: A.M | last post by:
Hi, How can I store a class (including all it's members) in session object? I already tried something like: Session = myClass; but it stores the value of myClass.ToString in it!!
4
by: Grigs | last post by:
Hello, I have a fairly simple class that I created and it works great. Basically it remembers a certain amount of strings. When a new one comes in the oldest drops off. The problem is, when I...
3
by: Tony Johansson | last post by:
Hello!! You may correct me if I have made any wrong assumptions. Below I have some simple classes. When you have this t.ToString() below it's the ToString() method in Object class that is...
6
by: bryanbabula | last post by:
I have a question about overriding i was wondering if anyone could help me with, or even suggesting a better/different way. I have no idea if this can even be done or not. I was wondering if there...
9
by: Zytan | last post by:
"A static member 'function' cannot be marked as override, virtual or abstract" Is it possible to make a static class member function (which is also static, obviously) that is an override to...
1
by: learning | last post by:
Hi how can I instaltiate a class and call its method. the class has non default constructor. all examples i see only with class of defatul constructor. I am trying to pull the unit test out from...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.