473,569 Members | 2,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4543
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.co m

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

Andreas :-)

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

"AWHK" <aw**@newsgroup .nospam> schrieb im Newsbeitrag
news:uW******** ******@tk2msftn gp13.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 NotImplementedE xception, 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
3761
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
3665
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 { /* ... */ }; template<class T> classSubject {
8
1297
by: Sunny | last post by:
Hi all, The code as follow: using System; class A { public virtual void F() {
5
1193
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
1314
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 have the test page open in multiple browsers, they are all getting the same data. I don't want this to happen. I have tried different uses of...
3
1720
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 called and when you have this t.GetType() below it's the GetType() method in the Object class that is called.
6
27778
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 was anyway to force a class to call a base class's method that it is overriding? Almost the same way you have to call a base class's constructor if...
9
5037
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 ToString()? Maybe it makes no sense to do such a thing... Zytan
1
2831
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 the product source code, but still want to execute them under nunit. I am trying this idea on nunit sample source code. Here is my class and the...
0
7701
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8130
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.