How to validate required fields in class properties?
<p>I wanted a simple way to ensure that some properties in a class contained values and/or were within a range (ie: not more than 50 characters long). I used the question and answer on <a href="https://stackoverflow.com/questions/21027865/how-to-validate-class-properties">How to validate Class properties?</a> unfortunately I was not able to get it to work. </p>

<p>To test it, I created a very simple WinForm example using C#. Even though I am doing everything the same, it never throws a validation exception when I apply incorrect values (ie: setting the age above the allowed limit). </p>

<p>Can someone explain why it does not throw the exception? It's as if the class doesn't know it is supposed to use the required attributes. </p>

<p><strong>Form1.cs</strong></p>

<pre><code>using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.ComponentModel.DataAnnotations;

namespace RequiredFieldsInClassExample {
public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }

    private void btnTest_Click(object sender, EventArgs e) {
        try {
            lstStatus.Items.Clear();
            lstStatus.Items.Add("Creating list of people");
            List&lt;Person&gt; CollectionOfPeople = new List&lt;Person&gt;();

            lstStatus.Items.Add("Creating a good person");
            Person Jeff = new Person();
            Jeff.Age = 33;
            Jeff.Firstname = "Jeff";
            Jeff.Lastname = "Jefferson";
            Jeff.GroupCode = "JJJ";

            CollectionOfPeople.Add(Jeff);

            lstStatus.Items.Add("Creating a bad person");
            Person Tim = new Person();
            Tim.Age = 444;
            Tim.Firstname = "";
            Tim.Lastname = "";
            Tim.GroupCode = "";

            CollectionOfPeople.Add(Tim);

            lstStatus.Items.Add("Done");
        } catch (ValidationException Exp) {
            MessageBox.Show(this, Exp.Message, "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        } catch (Exception Exp) {
            MessageBox.Show(this, Exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
    }
}
}
</code></pre>

<p><strong>Person.cs</strong></p>

<pre><code>using System.ComponentModel.DataAnnotations;

public class Person {
private int m_iAge = 1;
private string m_sFirstname = "Unknown";
private string m_sLastname = "";
private string m_sGroupCode = "AAA";

//[Required(ErrorMessage = "Age is a required field.")]
//[Range(1, 100, ErrorMessage = "A persons age must be between 1 and 100.")]
[Required, Range(1, 100)]
public int Age
{
    get { return m_iAge; }
    set { m_iAge = value; }
}

//[Required(ErrorMessage = "Firstname is a required field.")]
[Required]
public string Firstname
{
    get { return m_sFirstname; }
    set { m_sFirstname = value; }
}

public string Lastname
{
    get { return m_sLastname; }
    set { m_sLastname = value; }
}

//[StringLength(3)]
public string GroupCode
{
    get { return m_sGroupCode; }
    set { m_sGroupCode = value; }
}
}
</code></pre>