Error: Not all code paths return a value

Asked

Viewed 3,402 times

2

Follow the error:

"Logincliente(string, string)":not all source paths return a value

    class Conta
    {
        #region atributos
        public String nomeCliente { get; set; }
        public String numAgencia { get; set; }
        public String numConta { get; set; }
        public Boolean contaExiste { get; set; }
        public Decimal saldoCC { get; set; }
        public Decimal saldoPoupanca { get; set; }

        #endregion

    #region metodos

    public  Conta()
    {
        Console.WriteLine("Conta não existe");
    }

    public Conta(String znome, String zAgencia, String zConta, Decimal zsaldoCC, Decimal zsaldoPP)
    {
        nomeCliente = znome;
        numAgencia = zAgencia;
        numConta = zConta;
        saldoPoupanca = 0.0m;
        zsaldoCC = 0.0M;
        contaExiste = true;
    }

    public Conta LoginCliente( String Agencia, String Conta)
    {
        DAO.SQL conexao = new DAO.SQL();
        conexao.AbrirConexao();
        Conta conta = conexao.ConsultarCliente(Agencia, Conta);
        conexao.FecharConexao();
        if(conta != null)
        {
            return conta;
        }
  • People the mistake I’m getting is coming from Logincliente

  • 1

    What do you want to return if the account is void? The ConsultarCliente() receives the account as argument. And returns account as well. Can it be another account? Can it be null? This code has several weird things.

2 answers

8

The error happens because you are not explicitly returning anything. To fix it, I recommend you create a variable to do the return:

public Conta LoginCliente( String Agencia, String Conta)
{
    Conta retorno = null;
    DAO.SQL conexao = new DAO.SQL();
    conexao.AbrirConexao();
    Conta conta = conexao.ConsultarCliente(Agencia, Conta);
    conexao.FecharConexao();
    if(conta != null)
    {
        retorno = conta;
    }
    return retorno;
}

Or make the explicit return of what you want to return and return account if the condition is true:

public Conta LoginCliente( String Agencia, String Conta)
{
    DAO.SQL conexao = new DAO.SQL();
    conexao.AbrirConexao();
    Conta conta = conexao.ConsultarCliente(Agencia, Conta);
    conexao.FecharConexao();
    return conta != null ? conta : null;
}

I used the null just to illustrate better the solution, because there would be no reason to return null, and the account value will already be null on those occasions. But if you want to return null same, just do not do the if:

public Conta LoginCliente( String Agencia, String Conta)
{
    DAO.SQL conexao = new DAO.SQL();
    conexao.AbrirConexao();
    Conta conta = conexao.ConsultarCliente(Agencia, Conta);
    conexao.FecharConexao();
    return conta;
}
  • You can turn if/ternary into return direct which has the same effect for the illustrated code

  • Yes, as it was written at the end of the reply, I used the null just to illustrate better the solution, but I believe that the most appropriate would not be to return null.

  • I didn’t explain myself correctly, I meant to do return conta; directly.

  • @Isac Yes, I understood, you who did not understand me kkk. I edited my answer anyway. To make it easier to understand.

1

I made an improved code within the C standards#.

I also commented that you need to implant the pattern Disposable in the connection class to ensure that the connection is closed even if an exception is thrown. You weren’t a big fan of these connection managers, but the worst part is that someone made a wrong one, leaked it and everyone copies it wrong without understanding what’s going on there.

public Conta LoginCliente(string agencia, string conta) {
    DAO.SQL conexao = new DAO.SQL();
    conexao.AbrirConexao(); //esta classe deveria adotar o padrão Disposable
    Conta cliente = conexao.ConsultarCliente(agencia, conta);
    conexao.FecharConexao(); //se der exceção isto nunca será executado
    return cliente;
}

I put in the Github for future reference.

The reason for the error is that the return is in a conditional code snippet, so if you enter the if has a return, but if not enter has not, he needs to return a Conta whatever happens in the code, unless there’s an exception.

  • ps. conta is a parameter and just below is declaring an object of type Conta eponymous.

  • 1

    @Rovannlinhalis this whole code doesn’t make sense, I responded more because of this. I would rewrite everything, but that doesn’t work, so at least it solves some of his problems like that. I improved it, with your observation. Those names confused me. Thank you.

Browser other questions tagged

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