Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. force ToString() override in Deriv classed (already having a Base class)

force ToString() override in Deriv classed (already having a Base class)

Scheduled Pinned Locked Moved C#
questioncsharpdata-structureshelp
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    George Nistor
    wrote on last edited by
    #1

    Hi ...first problem in C# :) Basically I have a Base class and multiple derivs from Base. All derivs are part of a Tree, so the mytree has somthing like CNode Now my question is is any way to make (force), override ToString() in my derivs class. I will keep my all derivs class derived from Base. so I can't use another abstract class and derive from this also. I guess the only way is true by another interface, but how do I do this? (if I put a simple interface and having func replaced with another func..mToString() -> this will be virtual in my Base and make derivs:INewInterface {string mToString()} doesn't work either because the compiller will consider having definition from Base ) thx interface IBase { string ToString(); } class CBase { public override string ToString() { return "Base"; } } class CDeriv1 : CBase, IBase { public override string ToString() { return "Deriv1"; } } class Program { static void Main(string[] args) { CBase nod = new Deriv1(); } }

    K N P 3 Replies Last reply
    0
    • G George Nistor

      Hi ...first problem in C# :) Basically I have a Base class and multiple derivs from Base. All derivs are part of a Tree, so the mytree has somthing like CNode Now my question is is any way to make (force), override ToString() in my derivs class. I will keep my all derivs class derived from Base. so I can't use another abstract class and derive from this also. I guess the only way is true by another interface, but how do I do this? (if I put a simple interface and having func replaced with another func..mToString() -> this will be virtual in my Base and make derivs:INewInterface {string mToString()} doesn't work either because the compiller will consider having definition from Base ) thx interface IBase { string ToString(); } class CBase { public override string ToString() { return "Base"; } } class CDeriv1 : CBase, IBase { public override string ToString() { return "Deriv1"; } } class Program { static void Main(string[] args) { CBase nod = new Deriv1(); } }

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      To do this (which isn't wise) need to create an abstract class:

      abstract class CBase
      {
      public override abstract string ToString();
      }

      class CDeriv1 : CBase
      {
      public override string ToString()
      {
      return "Deriv1";
      }
      }

      class Program
      {
      static void Main(string[] args)
      {
      CBase nod = new Deriv1();
      }
      }

      But I think doing this to ToString() is a bad idea, you should either call your method something else, or just override the existing ToString() method.

      Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.

      1 Reply Last reply
      0
      • G George Nistor

        Hi ...first problem in C# :) Basically I have a Base class and multiple derivs from Base. All derivs are part of a Tree, so the mytree has somthing like CNode Now my question is is any way to make (force), override ToString() in my derivs class. I will keep my all derivs class derived from Base. so I can't use another abstract class and derive from this also. I guess the only way is true by another interface, but how do I do this? (if I put a simple interface and having func replaced with another func..mToString() -> this will be virtual in my Base and make derivs:INewInterface {string mToString()} doesn't work either because the compiller will consider having definition from Base ) thx interface IBase { string ToString(); } class CBase { public override string ToString() { return "Base"; } } class CDeriv1 : CBase, IBase { public override string ToString() { return "Deriv1"; } } class Program { static void Main(string[] args) { CBase nod = new Deriv1(); } }

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        You can mark the ToString method as abstract, however, a better design would be to mark it as virtual.


        I know the language. I've read a book. - _Madmatt

        K G 2 Replies Last reply
        0
        • N Not Active

          You can mark the ToString method as abstract, however, a better design would be to mark it as virtual.


          I know the language. I've read a book. - _Madmatt

          K Offline
          K Offline
          Keith Barrow
          wrote on last edited by
          #4

          It is already declared as virtual in object, but it won't force an override as the OP requests. I agree with your suggestion, that the OP should either use [the existing ToString()] virtual method. If it is strictly necessary to force derived classes to implement the method, abstract is needed, but it is better to use a different method so as not to conflict with object.ToString().

          Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.

          N 1 Reply Last reply
          0
          • K Keith Barrow

            It is already declared as virtual in object, but it won't force an override as the OP requests. I agree with your suggestion, that the OP should either use [the existing ToString()] virtual method. If it is strictly necessary to force derived classes to implement the method, abstract is needed, but it is better to use a different method so as not to conflict with object.ToString().

            Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.

            N Offline
            N Offline
            Not Active
            wrote on last edited by
            #5

            Keith Barrow wrote:

            It is already declared as virtual in object,

            That's true, I was thinking to add the new keyword to resolve the conflict, however, you're correct it won't force the override.


            I know the language. I've read a book. - _Madmatt

            1 Reply Last reply
            0
            • N Not Active

              You can mark the ToString method as abstract, however, a better design would be to mark it as virtual.


              I know the language. I've read a book. - _Madmatt

              G Offline
              G Offline
              George Nistor
              wrote on last edited by
              #6

              hi First I need all my CDeriv.. clasees to be derived from Base. I have to convert these classes to Base and put them in a tree. So CDeriv class has a base: CBase class non - abstract. class CBase //this on is NON abstract - i need intances for my Tree { public virtual string mToString(); // don't intend necessarly to force override Object.ToString(), i just need a virtual here } class CDeriv1 : CBase { public override string mToString() { return "Deriv1"; } } now having this I would like to force in CDeriv1 mToString to be overriden..by an interface? because to derive from an abstract I can't. I allready have one base class

              K 1 Reply Last reply
              0
              • G George Nistor

                Hi ...first problem in C# :) Basically I have a Base class and multiple derivs from Base. All derivs are part of a Tree, so the mytree has somthing like CNode Now my question is is any way to make (force), override ToString() in my derivs class. I will keep my all derivs class derived from Base. so I can't use another abstract class and derive from this also. I guess the only way is true by another interface, but how do I do this? (if I put a simple interface and having func replaced with another func..mToString() -> this will be virtual in my Base and make derivs:INewInterface {string mToString()} doesn't work either because the compiller will consider having definition from Base ) thx interface IBase { string ToString(); } class CBase { public override string ToString() { return "Base"; } } class CDeriv1 : CBase, IBase { public override string ToString() { return "Deriv1"; } } class Program { static void Main(string[] args) { CBase nod = new Deriv1(); } }

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                Other than using abstract, the base class should not know or care about what any derived classes do. But... you could probably have the base class throw an Exception, or maybe mark it as Obsolete.

                1 Reply Last reply
                0
                • G George Nistor

                  hi First I need all my CDeriv.. clasees to be derived from Base. I have to convert these classes to Base and put them in a tree. So CDeriv class has a base: CBase class non - abstract. class CBase //this on is NON abstract - i need intances for my Tree { public virtual string mToString(); // don't intend necessarly to force override Object.ToString(), i just need a virtual here } class CDeriv1 : CBase { public override string mToString() { return "Deriv1"; } } now having this I would like to force in CDeriv1 mToString to be overriden..by an interface? because to derive from an abstract I can't. I allready have one base class

                  K Offline
                  K Offline
                  Keith Barrow
                  wrote on last edited by
                  #8

                  The only way c# will force you to override is by declaring a method as abstract. To do this, the class itself must be abstract, what you ask for is impossible without an abstract base class. The base class can still have implementations of methods etc, so you can form your tree, you can also cast to an abstract class e.g. this is valid:

                  CDeriv1 foo = new CDeriv1();
                  CBase myInstance = foo;

                  Even if CBase is abstract So you can have a List<CBase> etc as a property to form the tree. Try making your current base class abstract and see what happens!

                  Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.

                  G 1 Reply Last reply
                  0
                  • K Keith Barrow

                    The only way c# will force you to override is by declaring a method as abstract. To do this, the class itself must be abstract, what you ask for is impossible without an abstract base class. The base class can still have implementations of methods etc, so you can form your tree, you can also cast to an abstract class e.g. this is valid:

                    CDeriv1 foo = new CDeriv1();
                    CBase myInstance = foo;

                    Even if CBase is abstract So you can have a List<CBase> etc as a property to form the tree. Try making your current base class abstract and see what happens!

                    Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.

                    G Offline
                    G Offline
                    George Nistor
                    wrote on last edited by
                    #9

                    :) ok, thanks I didn't know I can cast to a object from an abstract class. Thanks again

                    G 1 Reply Last reply
                    0
                    • G George Nistor

                      :) ok, thanks I didn't know I can cast to a object from an abstract class. Thanks again

                      G Offline
                      G Offline
                      George Nistor
                      wrote on last edited by
                      #10

                      hmmm...in fact that is not an object of an abstract class. Is my CDeriv object which is casted. :) ???

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups