最低年龄验证GUI C#

时间:2013-04-28 19:42:13

标签: c# validation user-interface

我正在尝试验证客户至少25岁,以便租车。 我相信我有正确的代码来计算我的客户类的年龄,但它实际上并没有在我的代码中验证它的应用

来自customer.cs的代码

 public int Age
    {
        get
        {
            // Get today's date
            DateTime currDate = DateTime.Now;

            // Get the difference in years
            int age = currDate.Year - birthdayPicker.Year;

            // Subtract another year if we're before the
            // birth day in the current year
            if (currDate.Month < birthdayPicker.Month || (currDate.Month == birthdayPicker.Month && currDate.Day < birthdayPicker.Day))
                age--;

            return age;
        }
    }

代码来自form1.cs(仍在使用应用按钮,但在我继续之前尝试验证年龄)

private void applyButton_Click(object sender, EventArgs e)
    {

        if (myCust.Age >= (25))
        {
            maketextbox.Text = myVehicle.Make;
            modelTextbox.Text = myVehicle.Model;
            yearTextbox.Text = Convert.ToString(myVehicle.Year);
            vinTextbox.Text = Convert.ToString(myVehicle.Vin);
            MessageBox.Show("Congratulations, you have been approved to rent a car!");
        }

        else
        {
            MessageBox.Show("You are not old enough to rent a car.");
        }

1 个答案:

答案 0 :(得分:0)

您的比较代码中可能存在错误:

 // Subtract another year if we're before the
 // birth day in the current year
 if (currDate.Month < birthdayPicker.Month || (currDate.Month >= birthdayPicker.Month && currDate.Day < birthdayPicker.Day))
            age--;

注意: == 运营商已更改为&gt; =