Re: Can a class's constructor call another constructor "later"?
From: Bruce Wood (brucewood_at_canada.com)
Date: 02/16/05
- Next message: David Dindorp: "Re: Can a class's constructor call another constructor "later"?"
- Previous message: csharp.general: "Re: Extracting name/value pairs from a string"
- In reply to: David Dindorp: "Re: Can a class's constructor call another constructor "later"?"
- Next in thread: David Dindorp: "Re: Can a class's constructor call another constructor "later"?"
- Reply: David Dindorp: "Re: Can a class's constructor call another constructor "later"?"
- Messages sorted by: [ date ] [ thread ]
Date: 16 Feb 2005 10:17:11 -0800
I suppose that this is a "pit"... if, as Alex said in his 'blog, the
coffee is wearing off. I find the current constructor behaviour
(particularly as he wrote it in his class) perfectly obvious:
public SecurityManager(User user) : this() {
this._user = user;
}
The call after the colon _always_ happens first. It has nothing to do
with "constructor overrides behave in the opposite way to method
overrides." I have no idea how he arrives at that conclusion. The
difference with constructor overrides is that the call order is
_fixed_: you can't insert a this() call or a base() call wherever you
like. It always comes first. The method override example he gave is
really a bit of a cheat because it calls the base method at the end,
illustrating nothing more than that when you override a method you can
choose to call the base method wherever you like, or not at all.
Constructor overrides, on the other hand, act like method overrides
that give you no choice where the base constructor is called: it is
always called at the beginning.
As well, I should point out that what Alex did in his first example
(the code with the bug in it, illustrated above) is bad constructor
form. Wherever possible, you should code constructors that take fewer
parameters to call those that take more parameters, _not_ the other way
around. His changed solution illustrates correct form:
public class SecurityManager {
public SecurityManager () : this(null) {
}
public SecurityManager(User user) {
this._user = user;
if(user == null) {
this._user = User.CurrentUser;
}
InitialiseData();
}
private User _user;
}
The constructor with no parameters calls the one-parameter constructor,
passing it a default value for the parameter: in this case, a null. I
call this "correct form" because it is much easier to follow what's
going on, and far more difficult to make the kinds of mistakes that
Alex pointed out in his 'blog.
- Next message: David Dindorp: "Re: Can a class's constructor call another constructor "later"?"
- Previous message: csharp.general: "Re: Extracting name/value pairs from a string"
- In reply to: David Dindorp: "Re: Can a class's constructor call another constructor "later"?"
- Next in thread: David Dindorp: "Re: Can a class's constructor call another constructor "later"?"
- Reply: David Dindorp: "Re: Can a class's constructor call another constructor "later"?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|