c# - How can I use a different ToString method in a derived class? -
I have a list of cars and I display a list in a list box using an overridden tooling method. I have received classes for cars (like hatchbacks), I need to display more information about the derived car model without losing the base tostring method.
I list the cars in a listback How can I show and how can I show "hatchback" details in a separate list box?
If I override the toasting in the model class, then I override the details, I am also trying to display for cars.
Thanks Sorry, C # My Cars Method
Public Override String Toasting () {Return Name + "[" + this.GetType () Name. Toastring () + "] is called" + type; } But when I make a derivative class, I sometimes want to use toasting from the forum and not always from the same base class.
The answer in brief is that what you describe can not be done in a derivative class virtual After override the method, you can not use the version in the original class from the derived class.
I would consider adding a new method or asset to the derived class to get the heckback - specific information. class car {public override string toasting ()} return "some basic information"; // whatever you want} class hatchback: car {public string hatchback info {return "get some hitchback info"} // return some details is only hatchback}} Collection of car objects that can actually hatchback, you can check the type of object and work accordingly:
Public Zero DoSomeStuffWithCars (list & gt; car & gt; ; Car) {foreach (in car C cars) Hatchback H = Hatchback C. If (H! = NULL) // Alternatively you can use the 'is' operator can do some stuff hatchback with {/ / H. Hatchback Infote} // logic applies to all cars here}}
Comments
Post a Comment