Error: not all paths return a value

14

I would like to understand why my code is generating the error

  

not all paths return to value

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace WindowsFormsApplication1
{
    public partial class painel : Form
    {
        public painel()
        {
            InitializeComponent();
        }

        public class componente
        {
            public int ID
            {
                get { return ID; }
                set { ID = value; }
            }

            private string nome;
            public string NOME
            {
                get { return nome; }
                set { nome = value; }
            }

            private string local_armazenamento;
            public string LOCAL_ARMAZENAMENTO
            {
                get { return local_armazenamento; }
                set { local_armazenamento = value; }
            }

            private string descricao;
            public string DESCRICAO
            {
                get { return descricao; }
                set { descricao = value; }
            }
        }

        private List<componente> ObterLista(string nome_componente)
        {
            MySqlConnection caminho = new MySqlConnection(@"SERVER=LOCALHOST;DATABASE=portfolio;UID=root;PASSWORD=vertrigo");
            List<componente> lista = new List<componente>();
            string query = "SELECT * FROM componentes ";

            MySqlCommand cmd = new MySqlCommand(query, caminho);
            caminho.Open();

            MySqlDataReader leitor = cmd.ExecuteReader();

            if (leitor.HasRows)
            {
                while (leitor.Read())
                {
                    componente componente = new componente();

                    componente.ID = Convert.ToInt32(leitor["id"]);
                    componente.NOME = leitor["nome"].ToString();
                    componente.LOCAL_ARMAZENAMENTO = leitor["local_armazenamento"].ToString();
                    componente.DESCRICAO = leitor["descricao"].ToString();
                    lista.Add(componente);
                }
                caminho.Close();
                return lista;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = ObterLista(button1.Text);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = ObterLista(button1.Text);
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

    }
}
    
asked by anonymous 03.05.2017 / 19:44

8 answers

19

Because not all execution paths return an expected value. Note that the only return exists in this code is within if , so it will only run conditionally. What happens if the code does not enter if ? Does he return anything? The code does not say to return something. Removing return from if solves the problem.

The list will be empty, you need to handle this where you use this method.

private List<componente> ObterLista(string nome_componente)
{
    MySqlConnection caminho = new MySqlConnection(@"SERVER=LOCALHOST;DATABASE=portfolio;UID=root;PASSWORD=vertrigo");
    List<componente> lista = new List<componente>();
    string query = "SELECT * FROM componentes ";

    MySqlCommand cmd = new MySqlCommand(query, caminho);
    caminho.Open();

    MySqlDataReader leitor = cmd.ExecuteReader();

    if (leitor.HasRows)
    {
        while (leitor.Read())
        {
            componente componente = new componente();

            componente.ID = Convert.ToInt32(leitor["id"]);
            componente.NOME = leitor["nome"].ToString();
            componente.LOCAL_ARMAZENAMENTO = leitor["local_armazenamento"].ToString();
            componente.DESCRICAO = leitor["descricao"].ToString();
            lista.Add(componente);
        }
        caminho.Close();
    }
    return lista;
}

Grounding my choice

You do not have to have a else in a code that will return the same thing that is inside if . Whenever you have two code numbers equal to if and the other within else you do not have to duplicate the code, take both if and else and eliminate redundancy.

You do not have to have return nor within else , which does not have to exist, nor within if , because whatever happens return lista would need to be returned.

I also do not think it's good to return null. At least nothing in the question indicates that this is desirable. If it were the case it would be better not to initialize the list ( List<componente> list; ) at first, just boot into if , so again it would only need to return the list once in code.

else is only increasing to cyclomatic complexity with no gain.

In addition to eliminating redundancy may be contributing to DRY , so the return becomes more canonical.

Has simpler ways to do it ( see more ) all the code and better style . This code will have problems if there is an exception in the middle of the operation. See how it should be .

    
03.05.2017 / 19:49
14

Your code needs to return a value for All cases, in its GetList class this does not happen, when the code enters the IF condition it has a return , but for the case of not entering ( Else ) it does not return anything, which generates the exception, just add an Else that returns a value null for example, but remember to treat this value so as not to generate errors in the rest of the code)

private List<componente> ObterLista(string nome_componente)
{
    MySqlConnection caminho = new MySqlConnection(@"SERVER=LOCALHOST;DATABASE=portfolio;UID=root;PASSWORD=vertrigo");
    List<componente> lista = new List<componente>();
    string query = "SELECT * FROM componentes ";

    MySqlCommand cmd = new MySqlCommand(query, caminho);
    caminho.Open();

    MySqlDataReader leitor = cmd.ExecuteReader();

    if (leitor.HasRows)
    {
        while (leitor.Read())
        {
            componente componente = new componente();
            componente.ID = Convert.ToInt32(leitor["id"]);
            componente.NOME = leitor["nome"].ToString();
            componente.LOCAL_ARMAZENAMENTO = leitor["local_armazenamento"].ToString();
            componente.DESCRICAO = leitor["descricao"].ToString();
            lista.Add(componente);
        }
        caminho.Close();

    }
    else
    {
        //Trata valor a ser retornado
    }

 return lista;

}

If you do not need a specific treatment for the null list, just do not use Else, changing

    }
    else
    {
        //Trata valor a ser retornado
    }

return lista;

by

}
return lista;

Avoiding unnecessary redundancies and always having a return of the method.

    
03.05.2017 / 19:52
11

Because of this:

        if (leitor.HasRows)
        {
            while (leitor.Read())
            {
                componente componente = new componente();

                componente.ID = Convert.ToInt32(leitor["id"]);
                componente.NOME = leitor["nome"].ToString();
                componente.LOCAL_ARMAZENAMENTO = leitor["local_armazenamento"].ToString();
                componente.DESCRICAO = leitor["descricao"].ToString();
                lista.Add(componente);
            }
            caminho.Close();
            return lista;
        }

If leitor has no rows, nothing is returned. After if , put a return null; that should resolve.

    
03.05.2017 / 19:48
9

Occurs because the GetList method is not returning result when reader.HasRows is equal to false. Change to:

private List<componente> ObterLista(string nome_componente)
    {
        MySqlConnection caminho = new MySqlConnection(@"SERVER=LOCALHOST;DATABASE=portfolio;UID=root;PASSWORD=vertrigo");
        List<componente> lista = new List<componente>();
        string query = "SELECT * FROM componentes ";

        MySqlCommand cmd = new MySqlCommand(query, caminho);
        caminho.Open();

        MySqlDataReader leitor = cmd.ExecuteReader();

        if (leitor.HasRows)
        {
            while (leitor.Read())
            {
                componente componente = new componente();

                componente.ID = Convert.ToInt32(leitor["id"]);
                componente.NOME = leitor["nome"].ToString();
                componente.LOCAL_ARMAZENAMENTO = leitor["local_armazenamento"].ToString();
                componente.DESCRICAO = leitor["descricao"].ToString();
                lista.Add(componente);
            }
            caminho.Close();
            return lista;
        }

        return null;

    }
    
03.05.2017 / 19:49
9

You are only returning the list in the GetList method, if you enter in if (reader.HasRows), if it does not enter, still the method should return something. Here's the code change:

    private List<componente> ObterLista(string nome_componente)
    {
        MySqlConnection caminho = new MySqlConnection(@"SERVER=LOCALHOST;DATABASE=portfolio;UID=root;PASSWORD=vertrigo");
        List<componente> lista = new List<componente>();
        string query = "SELECT * FROM componentes ";

        MySqlCommand cmd = new MySqlCommand(query, caminho);
        caminho.Open();

        MySqlDataReader leitor = cmd.ExecuteReader();

        if (leitor.HasRows)
        {
            while (leitor.Read())
            {
                componente componente = new componente();

                componente.ID = Convert.ToInt32(leitor["id"]);
                componente.NOME = leitor["nome"].ToString();
                componente.LOCAL_ARMAZENAMENTO = leitor["local_armazenamento"].ToString();
                componente.DESCRICAO = leitor["descricao"].ToString();
                lista.Add(componente);
            }
            caminho.Close();
            return lista;
        }
        else return lista;

    }
    
03.05.2017 / 19:49
6

At the signature of your method, you say that it returns a list of Component.

Try to put the statement of return out of if .

Code:

private List<componente> ObterLista(string nome_componente)
{
    MySqlConnection caminho = new MySqlConnection(@"SERVER=LOCALHOST;DATABASE=portfolio;UID=root;PASSWORD=vertrigo");
    List<componente> lista = new List<componente>();
    string query = "SELECT * FROM componentes ";

    MySqlCommand cmd = new MySqlCommand(query, caminho);
    caminho.Open();

    MySqlDataReader leitor = cmd.ExecuteReader();

    if (leitor.HasRows)
    {
        while (leitor.Read())
        {
            componente componente = new componente();

            componente.ID = Convert.ToInt32(leitor["id"]);
            componente.NOME = leitor["nome"].ToString();
            componente.LOCAL_ARMAZENAMENTO = leitor["local_armazenamento"].ToString();
            componente.DESCRICAO = leitor["descricao"].ToString();
            lista.Add(componente);
        }
        caminho.Close();
    }
    return lista;
}
    
03.05.2017 / 20:08
6

Try to put the return statement out of if .

private List<componente> ObterLista(string nome_componente)
{
    MySqlConnection caminho = 
    new MySqlConnection(@"SERVER=LOCALHOST;DATABASE=portfolio;UID=root;PASSWORD=vertrigo");

    List<componente> lista = new List<componente>();
    string query = "SELECT * FROM componentes ";

    MySqlCommand cmd = new MySqlCommand(query, caminho);
    caminho.Open();

    MySqlDataReader leitor = cmd.ExecuteReader();

    if (leitor.HasRows)
    {
        while (leitor.Read())
        {
            componente componente = new componente();

            componente.ID = Convert.ToInt32(leitor["id"]);
            componente.NOME = leitor["nome"].ToString();
            componente.LOCAL_ARMAZENAMENTO = leitor["local_armazenamento"].ToString();
            componente.DESCRICAO = leitor["descricao"].ToString();
            lista.Add(componente);
        }
        caminho.Close();
    }
    return lista;
}
    
03.05.2017 / 20:11
1

private List ObterLista(string nome_componente)
{
    MySqlConnection caminho = 
    new MySqlConnection(@"SERVER=LOCALHOST;DATABASE=portfolio;UID=root;PASSWORD=vertrigo");

List<componente> lista = new List<componente>();
string query = "SELECT * FROM componentes ";

MySqlCommand cmd = new MySqlCommand(query, caminho);
caminho.Open();

MySqlDataReader leitor = cmd.ExecuteReader();

if (leitor.HasRows)
{
    while (leitor.Read())
    {
        componente componente = new componente();

        componente.ID = Convert.ToInt32(leitor["id"]);
        componente.NOME = leitor["nome"].ToString();
        componente.LOCAL_ARMAZENAMENTO = leitor["local_armazenamento"].ToString();
        componente.DESCRICAO = leitor["descricao"].ToString();
        lista.Add(componente);
    }
    caminho.Close();
}
return lista;

}

    
04.05.2017 / 13:58