The Object of Your Affection | Age Restriction

Hi, I’ve finished this project up to the bonus task 12. But I can’t figure out how to put the age restriction they are asking for. “Convert the fields into private properties and add validation. For example, users must be at least 18 years of age.”

I know how to add validation in the setter method, but not sure how to stop the instance construction if the user does put in an age under 18. Do I need to use exception handling or is there a smarter way to do it?

https://www.codecademy.com/paths/build-web-apps-with-asp-net/tracks/build-web-apps-asp-net-intermediate-c-sharp/modules/learn-csharp-classes/projects/csharp-dating-profile

I am not sure exactly what the final code for that project looks like. But one way to validate fields on models in ASP.NET is to use Data Annotation Validators which are annotations that describe a set of requirements for a field that must be met for the model to be considered valid.

The documentation for Data Annotation Validators is here: Validation with the Data Annotation Validators (C#) | Microsoft Learn

Hopefully the information on that page will help get you started with using Data Annotation Validators in ASP.NET :slight_smile: