1

Lets say I have the following classes

class animal
{
   public string name;
   public animal(string name)
   {
      this.name = name;
   }
}

and also

class dog : animal
{
   public string breed;
   public dog(string name, string breed): base(name)
   {
       this.breed = breed;
   }
}

and finally

class poodle : dog
{
   public poodle(string name, string breed, int hairLength): base(name, breed)
   {
        //in here, name is = null for some reason.
   }
}

The issue is that when I have a three level hierarchy of inheritance, the base constructor (animal) seems to get called after the poodle constructor (name == null). But in the poodle constructor, I may of wanted to access some of the properties that were set in the base constructor, etc.

Does anyone know how this can be done, or perhaps, a much better coding practice to resolving this issue? (Only solution I can think of is not really using constructors but rather have a separate initialize() method.

Thanks!

Steve
  • 2,108
  • 4
  • 25
  • 36
  • 1
    http://stackoverflow.com/questions/140490/base-constructor-in-c-sharp-which-gets-called-first – Ken White May 30 '12 at 01:24
  • 1
    Erm, I'm pretty the base constructor executes first. – Kendall Frey May 30 '12 at 01:30
  • @Steve, please show code that does not work (and comment on a way that you expect it to work). So far `this.name` should be perfectly fine in inside Dog's constructor (BTW, consider following C# coding guidelines when writing sample code - i.e. classes should be starting with upper case like Animal and Dog) – Alexei Levenkov May 30 '12 at 01:41
  • `name`, `this.name`, or both are null? It seems to me that `name` must be simply being passed to you as null. Either that or your real code is more complex than your example, and is changing it somehow. Step through it in a debugger to see what's going on. If that doesn't make it obvious to you, show us the code you're actually using. – Tim S. May 30 '12 at 02:16
  • In the code you posted, as long as you don't pass a null as the `name`, `Console.WriteLine(this.name);` prints the name correctly. – Tim S. May 30 '12 at 02:21
  • I tried to create an instance of poddle putting breakpoints in each of the constructor, and they get called in the "right" order as expected (animal -> dog -> poodle). Have you tried this, or are you inferring it because name is null? BTW I don't think having the parameters with exactly the same name as the variables is a good idea. – Francesco Baruchelli May 30 '12 at 05:04

1 Answers1

2

This is a simple problem of visibility. Since you don't specify it explicitly, the name field is private, so it cannot be accessed by subclasses. You could make it protected like so:

protected string name;

This would be the simplest solution, but a better approach is to use a property:

protected string Name { get; set; }

And just use this instead of the field.

(Generally, it's considered best practice to not let fields escape the type in which they were declared. So once you need to access the field from outside the declaring type, you should consider using properties.)

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
  • Sorry, that wasn't the issue I was trying to explain. I've updated the sample above. For now lets say they're public. My problem isn't so much accessing the property, but rather I want the base constructor called first, so it can set the value, then in the dog constructor I can read the value. – Steve May 30 '12 at 01:26
  • 3
    @Steve, but that's *precisely* how it already works. The call to `base(name)` occurs *before* the rest of the code in your constructor. Are you getting an error? What makes you think it's not working? – Kirk Woll May 30 '12 at 01:27
  • Sorry, I just looked at my code a little closer and realized it only happens when I have three levels of hierarchy. Updating the question to reflect this now. – Steve May 30 '12 at 02:09
  • 1
    @Steve, again, how you want it work is *exactly* how it already works. Please explain in detail what precisely leads you to believe that the order of base constructor invocations works differently. – Kirk Woll May 30 '12 at 04:38