Hi,
I can't figure out how to get a method of an abstract class to refer to the values of a field stored in the subclasses.
Example in pseudo-code:
class Parent{
field _Description = "I'm the parent";
void PrintDescription(){
Console.WriteLine(this._Description);
)
}
class Child: Parent{
field _Description = "I'm the child";
}
If I instantiate the child class and call its PrintDescription() method, it prints
"I'm the parent"
How can I tell the 'PrintDescription()' method in the base class to always use the subclass's value for the field '_Description'?
Thanks.
Loading
AndrewPosted Aug 2, 2009, 5:44 PM
These classes are for collections of objects, each of which stores a database table name and list of column names and a few other things. There will be a dozen methods in the base class which should all behave the same in the inherited classes, but just refer to the values of the fields in the instances of those subclasses.
The whole point is that I don't want to define those methods in multiple places when they do exactly the same thing.
I suppose I could override just one method which obtains the field values from 'this' in the subclass...
Danatas GerviPosted Aug 2, 2009, 4:50 AM
Previos post - this is not overriding, it was hiding example. This is not so good. (The compiler will warn you "method ... hided ... use "new" operator ".
Kirtan PatelPosted Aug 2, 2009, 12:28 AM
class ParentClass
{
string Desc = "I m in Parent Class";
public void Display()
{
MessageBox.Show(Desc);
}
}
class ChildClass : ParentClass
{
string Desc= "I m in Child Class" ;
public void Display()
{
MessageBox.Show(this.Desc);
}
}