Inheritance Constructor Overload Question

  • Thread starter Thread starter needin4mation
  • Start date Start date
N

needin4mation

The code is taken from the book Professional C#:

abstract class GenericCustomer
{
private string name;
public GenericCustomer(string name)
{
this.name = name;
}...

class Nevermore60Customer : GenericCustomer
{
private uint highCostMinutesUsed;
public Nevermore60Customer(string name)
: base(name)
{
}

okay, I get this. it sends "name" back to the abstract class for
construction.

But what about overloading:

class Nevermore60Customer : GenericCustomer
{
public Nevermore60Customer(string name, string referrerName)
: base(name)
{
this.referrerName = referrerName;
}

private string referrerName;
private uint highCostMinutesUsed;
....

public Nevermore60Customer(string name)
: this(name, "<None>")
{
}

When someone sends just:

GenericCustomer customer = new Nevermore60Customer("Arabel Jones");

it will go to the proper constructor, the second one because it has
just a name, but we still wanted to have "<None>" as the string for
referrerName. My question:

Why does this last constructor have "this" instead of "base"?

Thank you for any help.
 
Because it's calling the constructor above it in the same class, it's
nothing to do with inheritance.

As an example (without inheritance)

public class Foo()
{
private object obj1;
private object obj2;

public Foo(object value1)
{
this.obj1 = value1;
}

public Foo(object value1, object value2):this(value1)
{
this.obj2 = value2;
}
}

The lower Ctor (object, object) calls the upper Ctor as part of it's
execution (actually it calls it first thing).

HTH

Simon
 
Basically, the call to:

GenericCustomer customer = new Nevermore60Customer("Arabel Jones");

Will end up calling the constructors in this order:

Object
GenericCustomer(string name)
Nevermore60Customer(string name, string referrerName)
Nevermore60Customer(string name)

In that order. The reason is that the overload of Nevermore60Customer
with one parameter calls the overload which takes two parameters. That
calls the GenericCustomer constructor, which in turn calls the constructor
for Object.

The last constructor has "this" instead of base, because whomever
implemented the class thought it was easier to route all the calls through
the overload with the most parameters (a concept I would generally agree
with).

Hope this helps.
 
The code is taken from the book Professional C#:

abstract class GenericCustomer
{
private string name;
public GenericCustomer(string name)
{
this.name = name;
}...

class Nevermore60Customer : GenericCustomer
{
private uint highCostMinutesUsed;
public Nevermore60Customer(string name)
: base(name)
{
}

okay, I get this. it sends "name" back to the abstract class for
construction.

But what about overloading:

class Nevermore60Customer : GenericCustomer
{
public Nevermore60Customer(string name, string referrerName)
: base(name)
{
this.referrerName = referrerName;
}

private string referrerName;
private uint highCostMinutesUsed;
...

public Nevermore60Customer(string name)
: this(name, "<None>")
{
}

When someone sends just:

GenericCustomer customer = new Nevermore60Customer("Arabel Jones");

it will go to the proper constructor, the second one because it has
just a name, but we still wanted to have "<None>" as the string for
referrerName. My question:

Why does this last constructor have "this" instead of "base"?

That's a technique known as "constructor chaining". Don't forget what
"this" refers to. In this particular situation, this(name, "<None>")
refers to the Nevermore60Customer(string name, string referrerName)
constructor of the Nevermore60Customer class.

Hope that helps,
 
Back
Top