Variable this.variable does not load the information for search, but for listing uploaded

Asked

Viewed 51 times

-1

The code is available at:Github

I got the Main Class:

    static void Main(string[] args)
        {

            ControleClienteService controleCliente = new ControleClienteService();
            controleCliente.CarregarCliente();
            ControleConsultaService controleConsulta = new ControleConsultaService();
            controleConsulta.CarregarConsulta();



    Console.WriteLine("==== Lista de Clientes ====");
                            Console.WriteLine(controleCliente.Listar());
                            Console.ReadKey();
                            break;
}

When I call the controleCliente.Listar(), he brings the information.

Client class:

public class ControleClienteService
    {
        private List<Cliente> Clientes;

        public bool CadastrarCliente(int qtdCliente, string nome, string endereco, string telefones, string email, string dataNascimento)
        {
            try
            {
                Cliente cliente = new Cliente
                {
                    Id = qtdCliente,
                    Nome = nome,
                    Endereco = endereco,
                    Telefones = telefones,
                    Email = email,
                    DataNascimento = Convert.ToDateTime(dataNascimento)
                };

                this.Clientes.Add(cliente);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public Cliente BuscarClienteId(int id)
        {
            Cliente cliente = new Cliente();
            if (this.Clientes != null)
            {
                cliente = this.Clientes.Where(x => x.Id == id).FirstOrDefault();
            }
            return cliente;

        }

        public bool CarregarCliente()
        {
            MClientes mClientes = new MClientes();
            this.Clientes = mClientes.PreencheClientes();
            return true;
        }

        public string Listar()
        {
            string retorno = "";
            foreach (Cliente C in this.Clientes)
            {
                retorno += C.Id.ToString() + "\n";
                retorno += C.Nome.ToString() + "\n";
                retorno += C.DataNascimento.ToString() + "\n";
                retorno += C.Telefones.ToString() + "\n";
                retorno += C.Endereco.ToString() + "\n";
                retorno += " \n";
            }
            return retorno;
        }

Query class:

public class MConsultas
    {
        List<Consulta> ConsultasClientes = new List<Consulta>();
        ControleClienteService ControleCliente = new ControleClienteService();
        ControleRestricaoAlimentarService RestricaoAlimentar = new ControleRestricaoAlimentarService();
        public List<Consulta> PreencheConsultas()
        {
            Consulta consulta = new Consulta
            {
                Id = 23,
                Cliente = ControleCliente.BuscarClienteId(23),
                RestricoesAlimentares = RestricaoAlimentar.BuscarRestricaoAlimentarId(23),
                Hora = Convert.ToDateTime("12:00"),
                Peso = 67,
                PercenteGordura = 34,
                Data = Convert.ToDateTime("19/12/2019")
            };
            // Adicionar na Lista
            ConsultasClientes.Add(consulta);

            return ConsultasClientes;
        }
    }

When you get to the calling part Cliente = ControleCliente.BuscarClienteId(23), Client class is empty, does not contain values. The controleConsulta.CarregarConsulta(); You should bring the Customer, however, always the Customer in the code is NULL.

How can I access the Customer, from the query class, equal I access from the Main class?

  • The estate Clientes is filled?

  • Yeah, I carry her in ControleClienteService controleCliente = new ControleClienteService();&#xA; controleCliente.CarregarCliente();&#Both that in Console.WriteLine(controleCliente.Listar());It works. Cliente = ControleCliente.BuscarClienteId(23),Also worked.

  • From what I understand of the code, there is no builder for the ControleClienteService and you’re not running the CarregarCliente() in the MConsultas. So, inside the boot new Consulta, where you perform the BuscarClienteId(23), the list Clientes is empty, so she’s only returning null...

  • How to make the Builder for the ControleClienteService?

  • Just create a method public NomeDaClasse(), that it already associates with constructor. You can get more details here

  • I’ll try to do it here.

  • @Felipeavelar worked, if you want to put as answer. For me to score.

  • You do not need to put "solved" in the title. As the answer below has been accepted (marked with ) this is enough to indicate that it has been solved

Show 3 more comments

1 answer

1


The problem, basically, is that the list of Clientes was never started, for this, just create a new builder in the class ControleClienteService, implementing the method CarregarCliente(). That should solve your problem.

Browser other questions tagged

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