How to use both base() and this() in constructor?

2 views
Skip to first unread message

jzhou

unread,
May 5, 2008, 7:48:30 AM5/5/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Suppose I have a class A:

class A
{
public A(string str){...}
public A(int n){...}
}

Now there is a class B inherits from class A. I add some members to
class B and I want to initialize these new members in a constructor
with no parameter.
In order to avoid duplication, I want the other constructors to call
the constructor (with no parameter) to initialize these new members,
meanwhile call the corresponding constructors in the base class (class
A) to do other initialization jobs. To clarify my question I write a
sample code:

class B : A
{
int nB;
bool bFlag;

public B() {nB=0; bFlag=true;}
public B(string str):base(str) /*I want to call this() here*/
{
// other things to do
}
public B(int n):base(n) /*I want to call this() here*/
{
// other things to do
}
}

But I didn't found any syntax which supports my idea. I wonder is that
possible or not?
Thanks a lot!

Cerebrus

unread,
May 5, 2008, 12:20:41 PM5/5/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting

jzhou

unread,
May 5, 2008, 1:11:25 PM5/5/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Thanks, Cerebrus:-)
But maybe you misunderstood my question.

I explain it again.

Now I could implement class B as follows:
class B : A
{
int nB;
bool bFlag;

public B() { nB=0; bFlag=true; }
public B(string str) : base(str)
{
nB = 0; bFlag = true;
// other things to do
}
public B(int n):base(n)
{
nB = 0; bFlag = true;
// other things to do
}
}

But I think that's not beautiful, because the code to initialize nB
and bFlag is duplicated serveral times.

On the other hand, if I use this() initializer, I could implement
class in the following manner:
class B : A
{
int nB;
bool bFlag;

public B() { nB=0; bFlag=true; }
public B(string str) : this()
{
/* I HAVE TO COPY THE CODE FROM class A ctor. TO HERE */
// other things to do
}
public B(int n) : this()
{
/* I HAVE TO COPY THE CODE FROM class A ctor. TO HERE */
// other things to do
}
}

The second implementation solves the problem of duplicating the code
in the parameterless ctor., however it leads to the duplication of the
base class' constructors.
The problem here is that I don't know the way to call both this() and
base() initializers (if possible).

The link you provide shows me how to use base() and this() seperately,
but I want to use them at the same point. I compose a pseudocode to
demostrate my idea:
class B : A
{
int nB;
bool bFlag;

public B() { nB=0; bFlag=true; }
public B(string str) : this(), base(str)
{
// other things to do
}
public B(int n) : this(), base(n)
> > Thanks a lot!- Hide quoted text -
>
> - Show quoted text -

Joshua Russo

unread,
May 5, 2008, 2:12:51 PM5/5/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I would suggest placing code common to all constructors in a private
initialization method. I don't believe you can explicitly trigger
constructors.

Josh

Andrew Badera

unread,
May 6, 2008, 12:11:17 PM5/6/08
to DotNetDe...@googlegroups.com
Sure you can. base() and base(param, param, ... param) etc.

All constructors should call back to a common base, or to an overloaded constructor method that eventually goes back to the common base constructor.

And I think the base actually gets called implicitly, but I could be wrong about that.
--
--Andy Badera
http://andrew.badera.us/ http://flipbitsnotburgers.blogspot.com/
and...@badera.us
(518) 641-1280
Google me: http://www.google.com/search?q=andrew+badera

Andrew Badera

unread,
May 6, 2008, 12:12:05 PM5/6/08
to DotNetDe...@googlegroups.com
OP: You don't want to use this() you want to use base().

Crisatunity (blog.crisatunity.com)

unread,
May 6, 2008, 12:50:06 PM5/6/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
On May 5, 1:12 pm, Joshua Russo <joshua.rupp...@gmail.com> wrote:
> ... I don't believe you can explicitly trigger
> constructors.

Huh? This is nonsense.

Glenn

unread,
May 6, 2008, 12:59:20 PM5/6/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I think what you want to do is this:

public A()
{
...
}

public B() : base()
{
...
}

public B(String txt): this()
{
...
}


If you construct it using B(String), it will call B() which calls
A().

...Glenn

Andrew Badera

unread,
May 6, 2008, 1:09:41 PM5/6/08
to DotNetDe...@googlegroups.com
Yeah, use this* not base* (today was a no-coffee Monday. Yes, I know it's Tuesday, but I had Thursday-Monday off :)

Glenn

unread,
May 6, 2008, 1:14:46 PM5/6/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I had to cut back to 1 cup a day. I was bouncing off the walls all
day!!

...Glenn

AstroDrabb

unread,
May 6, 2008, 1:05:22 PM5/6/08
to DotNetDe...@googlegroups.com
On Mon, May 5, 2008 at 1:11 PM, jzhou <El.Chr...@gmail.com> wrote:

Thanks, Cerebrus:-)
But maybe you misunderstood my question.

I explain it again.

Now I could implement class B as follows:
class B : A
{
   int nB;
   bool bFlag;

   public B() { nB=0; bFlag=true; }
   public B(string str) : base(str)
   {
        nB = 0; bFlag = true;
        // other things to do
   }
   public B(int n):base(n)
   {
        nB = 0; bFlag = true;
        // other things to do
   }
}

But I think that's not beautiful, because the code to initialize nB
and bFlag is duplicated serveral times.

<snip>

You can move the initialization outside of the ctor's like so:


class B : A
{
   int nB = 0;
   bool bFlag = true;
...
...
}


Or create a method that your ctor's call to handle the initialization.

You cannot have a ctor like

Myclass() : base(5), this(7)
{
...
}

Jim

jzhou

unread,
May 6, 2008, 1:34:16 PM5/6/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Thanks all! :-)
What I want is to keep the duplication of code to the minimal.
Because I see constructors could call other constructor by this()
initializer, and call the base class' constructor by base()
initializer, I think it could save some common code if I could use
both of them.
As Rusoo suggusted I think I could put the code to initialize the
derived class's members in a protected method, which is a good way to
keep off duplication, as the following code shows.

class B : A
{
int nB;
int bFlag;
public B()
{
Init();
}
public B(int n) : base(n)
{
Init();
}
public B(string str) : base(str)
{
Init();
}
protected void Init()
{
nB = 0; bFlag = true;
}
}

Thanks again=)
> > I explain it again.- Hide quoted text -

Joshua Russo

unread,
May 7, 2008, 9:22:20 AM5/7/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting

On May 6, 12:50 pm, "Crisatunity (blog.crisatunity.com)"
Ok, I did say "I don't believe", thus I'm not sure.

So can you do the following?

class A
{

public A()
{
...
}

public A(int ai)
{
this(); // to trigger the constructor above?
...
}

}

Glenn

unread,
May 7, 2008, 12:48:21 PM5/7/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Better yet, see my previous message on this.

Using "public A(int ai):this()" accomplishes the same thing.

If class A is your base class, use "public A(int ai):base()". This
insures that all inherited classes that construct with "base(int ai)"
will also execute "base()". Put all of your initialization calls
there and you aren't duplicating your initialization calls.

...Glenn

On May 7, 9:22 am, Joshua Russo <joshua.rupp...@gmail.com> wrote:
> So can you do the following?
> class A
> {
>
> public A()
> {
>   ...
>
> }
>
> public A(int ai)
> {
>   this(); // to trigger the constructor above?
>   ...
>
>
>
> }

Glenn

unread,
May 7, 2008, 12:52:08 PM5/7/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
You can make it simpler if your base class doesn't have an A(int) or
A(string) constructors. Just put your "init()" call into "B()" and
add ":this()" to your other constructors.

...Glenn

Joshua Russo

unread,
May 7, 2008, 2:15:46 PM5/7/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
That's cool, I've never used :this()

The example you give may work in some situations but it could get
tricky, in that you may not want the same logic if you directly
instantiate the base class. Even if the base class is abstract it may
not have the properties you want to set in your child class and
creating 3 classes seems to be a bit much.

Glenn

unread,
May 8, 2008, 2:14:58 PM5/8/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
That's why you use ":this()" so that your inherited class will call
your "no parameter" constructor to initialize all the variables for
your inherited class. That constructor then calls base() to
initialize the base class' parameters. That's why there's a
combination of this() and base() in the class constructors.

Where do you see three classes?

...Glenn

Crisatunity (blog.crisatunity.com)

unread,
May 8, 2008, 2:32:45 PM5/8/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
The concept to get yourself square on is that you can't call another
constructor of the same class in the body of a constructor. That's
why you use the outside the body syntax as Glenn demonstrated.
> > > - Show quoted text -- Hide quoted text -

Joshua Russo

unread,
May 8, 2008, 5:32:49 PM5/8/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
There may be more that you want to do in the default constructor than
what would be common logic to all constructors.

I understand what you were saying about base now. I thought you were
saying to put the common logic in the parent (base) class default
constructor. The three classes came from this and the fact that it may
not make sense to put the properties in the base class, and thus to
create an intermediate class.

Josh
Reply all
Reply to author
Forward
0 new messages