Constructors calling one another ...

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

A class can have more than one constructor, but can't one constructor call
another? It appears that the code below bombs and the error message claims
.... "denotes a 'class' which is not valid in the given context".

Thanks in advance! - Mark

public class cWhatever
{
public cWhatever(String str1)
{
whatever(str1, string.Empty);
}
public cWhatever(String str1, String str2)
{
//Do something
}
}
 
The other alternative is to put the code into a seperate function and call that from both constructors. That was you can execute the
initialization code wherever you like in the constructor.
 
Just my $.02, but I prefer the former method. Reduces the paths of code to
1 and means less changes to make if all paths ultimately lead to a single
ctor.

Michael Culley said:
The other alternative is to put the code into a seperate function and call
that from both constructors. That was you can execute the
 
However if you use a private method, then the paths lead there as well
(assuming your not doing anything else in the constructor.) Even if you
use "this", you can still have other code in the constructors that gets
executed after the "this" so there is no gaurentee of only one path, only
that some common code will get excecuted. So it's probably about the same
either way? Not advocating either way, just poking for knowledge myself.

--
William Stacey, DNS MVP

Keith Patrick said:
Just my $.02, but I prefer the former method. Reduces the paths of code to
1 and means less changes to make if all paths ultimately lead to a single
ctor.

Michael Culley said:
The other alternative is to put the code into a seperate function and
call
that from both constructors. That was you can execute the
 
But if you want the initialisation code run at the end of the constructor you've got no choice.

--
Michael Culley


Keith Patrick said:
Just my $.02, but I prefer the former method. Reduces the paths of code to
1 and means less changes to make if all paths ultimately lead to a single
ctor.

Michael Culley said:
The other alternative is to put the code into a seperate function and call
that from both constructors. That was you can execute the
 
Which is why all of my "other" ctors have no code in them; they are merely
overloads in lieu of default parameter support in the language.

William Stacey said:
However if you use a private method, then the paths lead there as well
(assuming your not doing anything else in the constructor.) Even if you
use "this", you can still have other code in the constructors that gets
executed after the "this" so there is no gaurentee of only one path, only
that some common code will get excecuted. So it's probably about the same
either way? Not advocating either way, just poking for knowledge myself.
 
That is nice if you can do it. Sometimes that is not possible or you have
to make the one ctor overly complex to handle all the different parms.
 
You cannot initialize readonly class members in a private function.
Those can only be assigned to in a ctor. Therefore, in some cases, it
is important that you redirect to another ctor instead of a private
function.

Scott Wagner
Seisint, Inc.
 
Why? If all your ctors call a master one, and the only copy of the init
code is there, it will run at the end of the construction process.

Michael Culley said:
But if you want the initialisation code run at the end of the constructor you've got no choice.
 
Back
Top