Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# format Enum strings
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
return string.Format("{0} {1} {2} {3} {4}", Name, ID, Age, Category,                   Gender);
 
whereas
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 9-Feb-13 13:06pm
Comments
PIEBALDconsult at 9-Feb-13 18:18pm
   
What do you expect to happen?
Sergey Alexandrovich Kryukov at 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 at 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 at 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 at 9-Feb-13 19:43pm
   
Yeah, that's not going to happen.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
leprechauny at 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 at 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)

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 Richard MacCutchan 145
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,712
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 9 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100