Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I use :base() and :this() correctly?
    primarykey
    data
    text
    <p>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.</p> <p>I have an abstract class called <code>EnteBase</code>, which I use as a base (<em>duh!</em>) for other two classes, <code>Regione</code> and <code>Provincia</code>.</p> <hr> <p><strong><code>EnteBase</code></strong>:</p> <pre><code>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 { [...] } } </code></pre> <hr> <p><strong><code>Regione</code></strong>:</p> <pre><code>public class Regione : EnteBase { public List&lt;Provincia&gt; Province { [...] } public Regione () : base() { this.Province = new List&lt;Provincia&gt;(); } public Regione ( string nome ) : this() { } public Regione ( string nome, int numeroComuni ) : this() { } public void AggiungiProvincia ( Provincia provincia ) { Province.Add( provincia ); } } </code></pre> <hr> <p><strong><code>Provincia</code></strong>:</p> <pre><code>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; } } </code></pre> <hr> <p>My questions are the following:</p> <ul> <li>Is it correct to use <code>:this()</code> in all constructors of the base class except the one with most parameters, with the others pointing towards the latter?</li> <li>Is it correct to use <code>:this()</code> pointing to the base constructor in the classes <code>Provincia</code> and <code>Regione</code> and then assign to the fields from inside the method itself?</li> </ul> <p>My problem rooted from the fact that I wanted to use both <code>:this()</code> and <code>:base()</code> 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 <a href="https://stackoverflow.com/questions/3797528/base-and-this-constructors-best-practices">this question</a> and <a href="https://stackoverflow.com/questions/3146152/c-if-a-class-has-two-constructors-what-is-the-best-way-for-these-constructors">this one</a>.</p> <p><strong>P.S.:</strong> in constructors, is it preferred to use <code>this.FieldName</code> or just <code>FieldName</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload