Error: Not all code paths return a value

0

The following is the error:

  

"CustomerLogon (string, string)": Not all code 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;
        }
    
asked by anonymous 12.07.2017 / 01:20

2 answers

5

The error happens because you are not returning anything explicitly. To fix it, I recommend that you create a variable to do 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 explicitly return what you want to return and return the 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 null just to better illustrate 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 even, just do not 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;
}
    
12.07.2017 / 01:30
1

I made an improved code within C # standards.

I also commented that you need to deploy the default disposable in the connection class to ensure that the connection is closed even if an exception is thrown. It was not much of a fan of these connection managers, but the worst of it is that someone made a wrong one, released it and everyone copied 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've placed it on GitHub for future reference .

The reason for the error is that return is in a conditional code snippet, so if you enter if it has return , but if you do not enter it does not, it must return Conta happen that happens in the code, unless you roll an exception.

    
12.07.2017 / 01:53