"AWHK" <awhk@newsgroup .nospam> wrote in message
news:uWUDubGjFH A.2444@tk2msftn gp13.phx.gbl...[color=blue]
> How can I force anyone who subclasses my class to override the ToString()
> method?
>
> Andreas :-)
>
>
>[/color]
Re: How can I force a class to override the ToString() method?
throw a NotImplementedE xception in your ToString() method to indicate that
ToString() should be overridden.
"AWHK" <awhk@newsgroup .nospam> schrieb im Newsbeitrag
news:uWUDubGjFH A.2444@tk2msftn gp13.phx.gbl...[color=blue]
> How can I force anyone who subclasses my class to override the ToString()
> method?
>
> Andreas :-)
>
>
>[/color]
Re: How can I force a class to override the ToString() method?
In message <uWUDubGjFHA.24 44@tk2msftngp13 .phx.gbl>, AWHK
<awhk@newsgroup .nospam> writes[color=blue]
>How can I force anyone who subclasses my class to override the ToString()
>method?[/color]
Surprisingly enough, this is allowed:
abstract class Foo
{
public abstract override string ToString();
}
class Bar:Foo
{
public override string ToString()
{
return "Override";
}
Re: How can I force a class to override the ToString() method?
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" <steve@otolith. demon.co.uk> wrote in message
news:kO+$KZVkoS 3CFwvc@otolith. demon.co.uk...[color=blue]
> In message <uWUDubGjFHA.24 44@tk2msftngp13 .phx.gbl>, AWHK
> <awhk@newsgroup .nospam> writes[color=green]
>>How can I force anyone who subclasses my class to override the ToString()
>>method?[/color]
>
> Surprisingly enough, this is allowed:
>
> abstract class Foo
> {
> public abstract override string ToString();
> }
> class Bar:Foo
> {
> public override string ToString()
> {
> return "Override";
> }
>
> }
>
>
> --
> Steve Walker[/color]
Re: How can I force a class to override the ToString() method?
> Surprisingly enough, this is allowed:[color=blue]
>
> abstract class Foo
> {
> public abstract override string ToString();
> }
> class Bar:Foo
> {
> public override string ToString()
> {
> return "Override";
> }
>
> }[/color]
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.
Re: How can I force a class to override the ToString() method?
In message <#LslZLJjFHA.14 80@TK2MSFTNGP10 .phx.gbl>, AWHK
<awhk@newsgroup .nospam> writes
[color=blue]
>Any idea how to force any subclasses to override operators?[/color]
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().
Comment