Estou fazendo um aplicativo de cadastro de cliente bem simples gostaria de saber como programar um método que calcula e retorna a idade (em anos) a partir do atributo AnoNascimento. Sei como fazer, por exemplo, um "public static int CalculaIdade(DateTime x)", mas para fazer um método construtor para calcular a idade a partir do ano de nascimento realmente não entendi.
Abaixo coloquei um print da tela e a parte do código que é interessante analisar:
List<Pessoa> listcadastro = new List<Pessoa>();
public class Pessoa
{
private string nome;
private string endereço;
private string ano_nascimento;
private string telefone;
public string Nome
{
get
{
return nome;
}
private set
{
nome = value;
}
}
public string Endereço
{
get
{
return endereço;
}
private set
{
endereço = value;
}
}
public string Ano_nascimento
{
get
{
return ano_nascimento;
}
private set
{
ano_nascimento = value;
}
}
public string Telefone
{
get
{
return telefone;
}
private set
{
telefone = value;
}
}
public Pessoa(string nome, string endereço, string ano_nascimento, string telefone)
{
this.nome = nome;
this.endereço = endereço;
this.ano_nascimento = ano_nascimento;
this.telefone = telefone;
}
}
private void bt_salvar_Click(object sender, EventArgs e)
{
listcadastro.Add(new Pessoa(txt_ano.Text, txt_nome.Text, txt_endereco.Text, txt_telefone.Text));
dataGridView1.DataSource = null;
dataGridView1.DataSource = listcadastro;
}
private void novoMenuItem_Click_1(object sender, EventArgs e)
{
txt_nome.Enabled = true;
txt_ano.Enabled = true;
txt_endereco.Enabled = true;
txt_telefone.Enabled = true;
if (bt_pjuridica.Checked)
{
txt_registro.Enabled = true;
txt_registro.Mask = "00,000,000/0000-00";
}
if (bt_pfisica.Checked)
{
txt_registro.Enabled = true;
txt_registro.Mask = "000,000,000-00";
}
}