QuestionsCategory: C# Application from Start to FinishC# Application Lesson 9 – creating a constructor for Prizemodel in the PrizeModel.cs class.
n.vanstrien asked 9 months ago

Hi Tim,
At around 1:12 in the video of lesson 9 (C# Application from Start to Finish) you are working on a new constructor for Prizemodel which takes 4 string arguments.
If I understand public properties correctly, you could have chosen to implement testing the input at the public property rather than in the constructor. I am wondering when to expand on the \”set\” instructions for the public property versus testing for valid data in the constructor. Below are two ways to implement setting a value for the public property PrizeAmount. Is there a preferred way of doing this? The lower of the two seems to be more robust when it comes to accepting multiple data types.
Thanks,
Niels

public decimal PrizeAmount {get; set;}

or

private decimal _prizeAmount;
public decimal PrizeAmount
{
  get => _prizeAmount;
  set
  {
    if (value = string)
    {
      decimal PrizeAmountValue = 0;
      decimal.TryParse(value, out PrizeAmountValue)
      _prizeAmount = PrizeAmountValue
    }
    else if (value = decimal)
    {
      _prizeAmount = value
    }
  }
}
1 Answers
Tim Corey answered 9 months ago

Here’s the problem with your solution: you cannot accept a string value as the setter for a decimal type. PrizeAmount is a decimal so you cannot pass in a string to it to be parsed. That is why I did the work in the method instead of in the property itself.

Your Answer