Call base class override method from base constructor C# -


i want have class have indexer , number of fields in following example:

public abstract class arecord {             public abstract double this[int index] { get; }     public abstract int numberoffields { get; } }  public class record : arecord {     public double field1{ get; private set; }     public double field2{ get; private set; }      public override int numberoffields { { return 2; } }      public record(double[] records)     {         if (records.count() != numberoffields) // problem here. when calling derived class numberoffields=3!             throw new argumentexception();         this.field1= records[0];         this.field2 = records[1];     }      public override double this[int index]     {         { throw new notimplementedexception(); }     }      }  public class childrecord : record {     public double field3 { get; private set; }      public override int numberoffields { { return 3; } }      public childrecord(double[] records)         : base(new double[] { records[0], records[1] })     {         if (records.count() != numberoffields)             throw new argumentexception();         this.field3 = records[2];     }      public override double this[int index]     {         { throw new notimplementedexception(); }     }      }  public static class testrecord {     public static void createrecord()     {         var record = new childrecord(new double[]{1.0,1.5,2.5}); // not working     } } 

this example crashes because of polymorphic call numberoffields childrecord inside constructor of record.

as far know, can use new insted of override solve problem, in case cannot declare numberoffields abstract in base class (which needed).

what proper way solve problem? wrong design?

the way you've formulated this, cannot possibly work intended. assume did (through sort of magic) work way think should, , able create childrecord:

var record = new childrecord(new double[] { 1.0, 1.5, 2.5 }); 

what expect value of record.numberoffields be? 2 or three? particular object cannot childrecord numberoffields == 3 , @ same time record numberoffields == 2. result of numberoffields implementation of instantiated class, regardless of whether type record arecord, record or childrecord.

to put way: makes no sense expose arecord.numberoffields outside, there no 1 correct answer -- isn't meaningful concept.

if abstain exposing it, can validation (with indexers , properties omitted):

public abstract class arecord {     public abstract double this[int index] { get; } }  public class record : arecord {     private const int numberoffields = 2;      public record(double[] records)     {         if (records.count() != numberoffields)             throw new argumentexception();         this.field1 = records[0];         this.field2 = records[1];     } }  public class childrecord : record {     private const int numberoffields = 3;      public childrecord(double[] records)         : base(new double[] { records[0], records[1] })     {         if (records.count() != numberoffields)             throw new argumentexception();         this.field3 = records[2];     } } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -