468,135 Members | 1,438 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,135 developers. It's quick & easy.

Inheritance Constructor Overload Question

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.

Sep 8 '06 #1
3 8092
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
<ne***********@gmail.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
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.

Sep 8 '06 #2
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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ne***********@gmail.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
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.

Sep 8 '06 #3
ne***********@gmail.com wrote:
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,

--
Sean
Sep 8 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Tony Johansson | last post: by
45 posts views Thread by Ben Blank | last post: by
3 posts views Thread by VJ | last post: by
36 posts views Thread by Pacific Fox | last post: by
2 posts views Thread by Jeff | last post: by
27 posts views Thread by didacticone | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.