-1

I tried this way to define custom defined property but I wonder how to handle if any of the dependent properties have null value.

public class ObtainedMarksDetail
     {
         private decimal? _percentage;
         public decimal? TheoryTotal { get; set; } 
         public decimal? PracticalTotal { get; set; }
         public decimal? TheoryFullMarksTotal { get; set; }
         public decimal? PracticalFullMarksTotal { get; set; }
         public decimal? TheoryPassMarksTotal { get; set; }
         public decimal? PracticalPassMarksTotal { get; set; }  

         public decimal? TotalPercentage
         {

             get { return _percentage=(TheoryTotal + PracticalTotal) / (TheoryFullMarksTotal + PracticalFullMarksTotal); }
             set { _percentage = value; }
         }

     }

My question is how to set default value for dependednt properties i.e. I tried this way but it shows error.

 public decimal? TheoryTotal { get; set; }=0.0;

Also what else if TheoryTotal throws null value how can I handle in the composite getter of property TotalPercentage

  public decimal? TotalPercentage
     {

         get { return _percentage=(TheoryTotal + PracticalTotal) / (TheoryFullMarksTotal + PracticalFullMarksTotal); }
         set { _percentage = value; }
     }
  • "I tried this way but it shows error...". That's because you are casting a double to a decimal, which the compiler won't let you do implicitly. Change that line to public decimal? TheoryTotal { get; set; }=0.0m; and it'll compile. The code is still horrific, with all those "nullables", but it'll compile... – David Arno Dec 10 '18 at 12:10
3
public decimal? TotalPercentage
{
    get { return _percentage = (TheoryTotal + PracticalTotal) / (TheoryFullMarksTotal + PracticalFullMarksTotal); }
    set { _percentage = value; }
}

The behavior of this property is kind of overdefined, and it needs more thought. If the client code assigns the new value, and immediately reads the TotalPercentage back, then it will not get the same value back. Furthermore, the getter assigns (!) a new value to the _percentage field. This will smell confusing. This behavior could and should be designed one of the following ways.

  • Make the property read-only by removing the setter.
  • Add the flag inside your class which will tell if the percentage is externally assigned or calculated. Perhaps, set the flag in the setter when the client code assigns the percentage. In the getter, return the assigned or calculated value based on the flag.
  • Create separate classes for manual assigned percentage and calculated percentage.
  • Thank you for your reply. I solved this problem as you mentioned but I wonder how can we set default value in getter and setter instead? – Ishwor Khanal Dec 8 '18 at 4:52
  • 1
    @IP Kaal Use proper private back values that you can assign defaults to or initialize in a constructor. – Martin Maat Dec 8 '18 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.