C# error - Not all code paths return value

陌路散爱 提交于 2021-02-17 07:16:12

问题


So guys I know that have another posts like this with this name,but the other posts didn't help me find some solution for my problem. I have 1 Form and 4 Class with methods,and It's getting problem in the Class called Costs,that get's all the other 3 Class and put in him. I will post here the four classes.

First Class - Alimentaçao

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Projeto_CustosViagem
{
    class Alimentaçao
    {
        private String descriçao { get; set; }
        private double valorTotal { get; set; }
        private String nomeRestaurante { get; set; }

        public Alimentaçao()
        {
            valorTotal = 0;
        }

        public void calcularDespesa(int qtdeRef)
        {
            valorTotal = qtdeRef * 18;
        }

        public void listarDespesa()
        {
            MessageBox.Show("Descrição : " + descriçao + "Valor Total = " + valorTotal + "Nome do Restaurante : " + nomeRestaurante);
        }


    }
}

Second Class - Transporte

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Projeto_CustosViagem
{
    class Transporte
    {
        private double kmPercorrida { get; set; }
        private double valorPedagios { get; set; }
        private double valorTotal { get; set; }


        public Transporte() {
            kmPercorrida = 0;
            valorPedagios = 0;
            valorTotal = 0;
        }

        public void calcularDespesa()
        {
            valorTotal = (kmPercorrida * 8);
        }


        public void listarDespesa()
        {
            MessageBox.Show("Km Percorridos : " + kmPercorrida + "Valor dos Pedagios : " + valorPedagios + "Valor Total : " + valorTotal);
        }

    }


}

Third Class - Hospedagem

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Projeto_CustosViagem
{
    class Hospedagem
    {
        private String nomeHotel { get; set; }
        private double valorDiaria { get; set; }
        private int qtdeDiarias { get; set; }
        private double valorTotal { get; set; }


        public Hospedagem()
        {
            valorDiaria = 0;
            qtdeDiarias = 0;
            valorTotal = 0;
        }


        public void calcularDespesa()
        {
            valorTotal = (qtdeDiarias * valorDiaria);
        }

        public void listarDespesa()
        {
            MessageBox.Show("Nome do Hotel : " + nomeHotel + "Valor da Diária : " + valorDiaria + "Quantidade de Diárias : " + qtdeDiarias + "Valor total : " + valorTotal);
        }
    }
}

Four Class - Custos(where are the problem)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Projeto_CustosViagem
{
    class Custos
    {
        public double totalViagem(Alimentaçao A, Transporte T, Hospedagem H)
        {

        }
    }
}

If you guys can help me,I will thanks.The problem says that not all code paths return a value.


回答1:


your method "totalViagem" doesnt return anything, you have set its return type as double but it returns nothing, therin lies your problem



来源:https://stackoverflow.com/questions/33977741/c-sharp-error-not-all-code-paths-return-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!