Builder without parameters is not working, is leaving the object as null

Asked

Viewed 104 times

-3

I have a class with two constructors. The empty constructor (no arguments) is not working. Script shows no error. One of the constructors receives the parameters nome and limitedecredito; and another, empty, initializes the CPF.

Turns out the countryside cpf (TCPF) is getting the value NULL.

Builder of the customer class:

public Cliente():base()
{ 
}

Main Program:

(...)
case 1:
    Console.Clear();
    c1 = new Cliente();
    Console.Write("CPF:");
    CPF = Console.ReadLine();
    if (c1.setCPF(CPF) == false)
    {
        Console.WriteLine("CPF inválido");
        Console.ReadKey();
        break;
    }
    Console.Write("Nome:");
    string nome = Console.ReadLine();
    Console.Write("Limite de credito R$:");
    double limitecredito = double.Parse(Console.ReadLine());
    c1 = new Cliente(nome, limitecredito);
    LC.Add(c1);
    Console.Write("Cadastro realizado com sucesso");
    Console.ReadKey();
    break;
(...)

Class Cliente:

private double limitecredito;

public Cliente():base()
{ 
}

public Cliente(string n, double limitec) : base(n)
{
    limitecredito = limitec;
}

Class Pessoa (Mother):

private string nome;
private TCPF cpf;

public Pessoa()
{
    cpf = new TCPF();
}

public Pessoa(string n)
{
    cpf = new TCPF();
    nome = n;
}

public string Nome
{
    get { return nome; }
}

public bool setCPF(string X)
{
    return cpf.ValidaCpf(X);
}

public string Cpf
{
    get { return cpf.Cpf; }
}
  • Where is the error?

  • https://dotnetfiddle.net/U84zk8 see this, and error reading ?

  • The program is without any error happens that I created two constructs one to store the attributes name and limit credit and one to start the CPF happens that the CPF(object) is getting the value NULL

1 answer

1

If with such a simple flow it was possible to reach this complexity of this size, the destination of this program will surely be the disposal.

A problem is quite trivial, but the path you have chosen to walk is truly painful.

About your question and the code you posted, there are several things going on, minus what her title suggests:

  1. At first you instate a customer (c1 = new Cliente();), receives Cpf and the name, but after the user informs the credit limit you replace the previous instance of the client (c1 = new Cliente(nome, limitecredito););
  2. Your method setCpf nay arrow Cpf. It validates. What if the ValidarCpf arrow he doesn’t know who he is even in this card game;
  3. Your cpf(TCPF) is not null. It has an instance, but its properties are probably not initialized (we did not have access to the TCPF class to confirm);

In conclusion, the builders are running smoothly, but you are making a tremendous mess in programming logic and in the use of OO concepts. In this example of implementing your code, you can see that the instance is 'live', but the value of the CPF itself is null.

This same program, (considering its line of modeling and programming) could be done a little simpler and with less rewriting.

I hope I’ve helped.

Browser other questions tagged

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