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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AWHK

    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 :-)



  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: How can I force a class to override the ToString() method?

    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]
    - mvp@spam.guard. caspershouse.co m

    "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]


    Comment

    • cody

      #3
      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]


      Comment

      • Steve Walker

        #4
        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";
        }

        }


        --
        Steve Walker

        Comment

        • AWHK

          #5
          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]


          Comment

          • Kevin Yu [MSFT]

            #6
            Re: How can I force a class to override the ToString() method?

            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."

            Comment

            • cody

              #7
              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.


              Comment

              • Steve Walker

                #8
                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().

                --
                Steve Walker

                Comment

                Working...