To keep the solution of this in a way that meets the scope of your exercise, I made these changes to your code, but I did it in a way that is simple enough for your teacher not to think bad and of course, for you to learn too.
Since I don’t know how far your teacher has already taught you, I’m not going to modify anything in your class, I believe the goal of doing the "_" before the names is to create public property using private variables, but I’ll leave your code anyway.
I am creating here a static method that will return you an instance of the Person class, only if all fields are validated. Otherwise, it will return to you null
Notice that I put the constructor as private
so the programmer is required to call the method GetInstancia
public class Pessoa
{
public string _nome;
public string _sobrenome;
public string _CPF;
public DateTime _dataNascimento;
private Pessoa(string nome, string sobrenome, string CPF, DateTime dataNascimento)
{
_nome = nome;
_sobrenome = sobrenome;
_CPF = CPF;
_dataNascimento = dataNascimento;
}
public static Pessoa GetInstancia(string nome, string sobrenome, string CPF, DateTime dataNascimento)
{
bool construir = true;
if (nome == "")
{
Console.WriteLine("O nome é obrigatório.");
construir = false;
}
if (sobrenome == "")
{
Console.WriteLine("O sobrenome é obrigatório");
construir = false;
}
if (CPF == "")
{
Console.WriteLine("O CPF é obrigatório.");
construir = false;
}
else if (CPF.Length != 11)
{
Console.WriteLine("O CPF é inválido.");
construir = false;
}
if (dataNascimento == DateTime.MinValue)
{
Console.WriteLine("A data é obrigatória.");
construir = false;
}
if(construir)
{
return new Pessoa(nome, sobrenome, CPF, dataNascimento);
}
else
{
return null;
}
}
}
Remember however that the purpose of this is only didactic!
I do not recommend putting Console.WriteLine
within the class.
However, as the goal is to keep the simplicity, I see no problems.
Example of how to consume this method:
Pessoa _pessoa = Pessoa.GetInstancia("teste", "teste", "1234", DateTime.Now);
if(_pessoa != null)
{
//Código para adicionar em uma lista.
}
Make a validator for Cpf, you can find that you have several examples. Vc can also use Fluent Validation to validate your class
– Marco Souza
What is the purpose of the exercise? This influences the approach that we can recommend. Note: those attributes started with "_" should not be private?
– Leandro Angelo
Well, since you don’t have a default constructor, these fields are already required in the class, which I think you’re actually looking to check if the constructor parameters have valid values. You can make a Factory method that returns the instance of the object only if the parameters are correctly filled and leave this constructor you made private, forcing the programmer to access the Factory method
– Zorkind
However, if your goal is to make a validator for the user, you can also use the concept I mentioned about Factory, but returning a list of messages to be presented to the user in case some field is not correct. The implementation of this may be a little too sophisticated for the scope of your exercise however.
– Zorkind