I've already searched about this issue both on SO and other websites but I haven't managed to find (or come to) a solution for my case.
I have an abstract class called EnteBase
, which I use as a base (duh!) for other two classes, Regione
and Provincia
.
EnteBase
:
public abstract class EnteBase
{
public EnteBase ()
: this( "Sconosciuto", 0 )
{
}
public EnteBase ( string nome )
: this( nome, 0 )
{
}
public EnteBase ( string nome, int numeroComuni )
{
this.Nome = nome;
this.NumeroComuni = numeroComuni;
}
private string nome;
public string Nome
{
[...]
}
private int numeroComuni;
public int NumeroComuni
{
[...]
}
}
Regione
:
public class Regione : EnteBase
{
public List<Provincia> Province
{
[...]
}
public Regione ()
: base()
{
this.Province = new List<Provincia>();
}
public Regione ( string nome )
: this()
{
}
public Regione ( string nome, int numeroComuni )
: this()
{
}
public void AggiungiProvincia ( Provincia provincia )
{
Province.Add( provincia );
}
}
Provincia
:
public class Provincia : EnteBase
{
private string sigla;
public string Sigla
{
[...]
}
public Provincia ()
: base()
{
}
public Provincia ( string nome )
: this()
{
this.Nome = nome;
}
public Provincia ( string nome, int numeroComuni )
: this()
{
this.Nome = nome;
this.NumeroComuni = numeroComuni;
}
public Provincia( string nome, int numeroComuni, string sigla)
: this()
{
this.Nome = nome;
this.NumeroComuni = numeroComuni;
this.Sigla = sigla;
}
}
My questions are the following:
- Is it correct to use
:this()
in all constructors of the base class except the one with most parameters, with the others pointing towards the latter? - Is it correct to use
:this()
pointing to the base constructor in the classesProvincia
andRegione
and then assign to the fields from inside the method itself?
My problem rooted from the fact that I wanted to use both :this()
and :base()
in every method. When I discovered that it was not possible I looked for a solution, but I couldn't find a way to apply what I saw in this question and this one.
P.S.: in constructors, is it preferred to use this.FieldName
or just FieldName
?