force ToString() override in Deriv classed (already having a Base class)
-
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(); } }
-
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(); } }
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 existingToString()
method.Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
-
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(); } }
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
-
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
It is already declared as
virtual
inobject
, but it won't force an override as the OP requests. I agree with your suggestion, that the OP should either use [the existingToString()
] 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 withobject.ToString()
.Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
-
It is already declared as
virtual
inobject
, but it won't force an override as the OP requests. I agree with your suggestion, that the OP should either use [the existingToString()
] 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 withobject.ToString()
.Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
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
-
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
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
-
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(); } }
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.
-
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
The only way c# will force you to
override
is by declaring a method asabstract
. 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 aList<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.
-
The only way c# will force you to
override
is by declaring a method asabstract
. 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 aList<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.
:) ok, thanks I didn't know I can cast to a object from an abstract class. Thanks again
-
:) ok, thanks I didn't know I can cast to a object from an abstract class. Thanks again
hmmm...in fact that is not an object of an abstract class. Is my CDeriv object which is casted. :) ???