How do I make a field mandatory?

Asked

Viewed 830 times

0

I am mounting an Engineering Management Application(Consoleapp) p/college... I would like to know how to make mandatory the entry of the fields Name/Surname/CPF/dataNascization when the user is adding a new person? Ah and I would like to know how to put the CPF to have 11 characters?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CSharpUltimateASS;

namespace CSharpUltimateASS
{
    public class Pessoa
    {
        public string _nome;
        public string _sobrenome;
        public string _CPF;
        public DateTime _dataNascimento;

        public Pessoa(string nome, string sobrenome, string CPF, DateTime dataNascimento)
        {
            _nome = nome;
            _sobrenome = sobrenome;
            _CPF = CPF;
            _dataNascimento = dataNascimento;

        }
    }
}
  • Make a validator for Cpf, you can find that you have several examples. Vc can also use Fluent Validation to validate your class

  • What is the purpose of the exercise? This influences the approach that we can recommend. Note: those attributes started with "_" should not be private?

  • 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

  • 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.

1 answer

1


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.
}
  • 1

    Thank you very much, @Zorkind (yes I’m new to both overflow and programming... solved my problem... vlw even xD)

  • Boot ai as answer accepted _ Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.