Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
public class Eagle : Bird
    {
        public Eagle():base()
        { }

        public Eagle(string name, string id, double age, CategoryType category, GenderType gender, BirdSpecies birdSpecie)
            : base(name, id, age, category, gender)
        {
            birdSpecie = BirdSpecies.Eagle;
        }

        public override string ToString()
        {
            //return string.Format("{0}", base.ToString());
            //return string.Format("{0} {1} {2} {3} {4}", Name, ID, Age, Category,                   Gender);
        }


Why is it that the second formatted string works
C#
return string.Format("{0} {1} {2} {3} {4}", Name, ID, Age, Category,                   Gender);


whereas
C#
return string.Format("{0}", base.ToString());


fails to do so. Is it due to some mix-up with the constructors? Is the default empty constructor that is causing the error?
Posted
Comments
PIEBALDconsult 9-Feb-13 18:18pm    
What do you expect to happen?
Sergey Alexandrovich Kryukov 9-Feb-13 19:18pm    
It really does not matter, taking into account base class based "implementation". So the situation is worse: this code does not make sense no matter why it would be written. Please see my answer where I explain it.
—SA
leprechauny 9-Feb-13 18:54pm    
Having the input from run-time being print as name, age, cat, gender simply by calling base, instead of the second. Basically, I'm wondering why the second one works and not the first one, ought they not to work in the same way? What am I missing?
Sergey Alexandrovich Kryukov 9-Feb-13 19:19pm    
Isn't that obvious? If not, I would question if you understand virtual methods at all, maybe not. I answered.
—SA
PIEBALDconsult 9-Feb-13 19:43pm    
Yeah, that's not going to happen.

1 solution

No, there is no "mix-up". The problem is trivial. These two code lines have almost nothing in common, so the "answer" would depend on what you want to achieve. Who told you base.ToString() is supposed to do anything useful, especially in you did not override this method in your base class.

To help your to understand what's going on, I'll tell you: if you return string.Format("{0}", base.ToString()); from your ToString implementation functionally is strictly equivalent to not overriding this method at all. You simply try to return exact same value the base class returns. Isn't that obvious?

—SA
 
Share this answer
 
Comments
leprechauny 9-Feb-13 20:06pm    
Well, it is overridden in the base class as well. Somehow I thought this would nicely follow in a descending manner when working with polymorph and inheritance. Thanks, though. For clarifying it.
Sergey Alexandrovich Kryukov 9-Feb-13 20:13pm    
You are welcome,
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900