【问题标题】:C# error - Not all code paths return valueC# 错误 - 并非所有代码路径都返回值
【发布时间】:2015-11-28 23:41:06
【问题描述】:

所以,我知道有人用这个名字发布过类似的帖子,但其他帖子并没有帮助我找到解决问题的方法。 我有 1 个表格和 4 个带方法的类,在名为 Costs 的类中出现问题,得到所有其他 3 个类并放入他。 我将在这里发布四个类。

头等舱 - 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);
        }


    }
}

二等车 - 运输

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);
        }

    }


}

三等奖 - 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);
        }
    }
}

四类-Custos(问题出在哪里)

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)
        {

        }
    }
}

如果你们能帮助我,我会感谢的。问题是不是所有的代码路径都返回一个值。

【问题讨论】:

  • 如何阅读有关此类微不足道的文档?
  • “并非所有代码路径都返回值”是比较明显的编译器错误之一...
  • 如果这些话对您没有帮助,请务必记下数字 CS0161(CS 表示 C# 编译器)。 MSDN 应该能够告诉您有关 C# 错误的信息。如果您使用 IDE,它甚至可能会为您查找(尝试 F1)。
  • 好吧,对不起,伙计们,我知道这个问题有点菜鸟,哈哈,我一开始不明白,但这个错误永远不会再发生了。所以谢谢你的帮助(或不是 xxbbcc 和 Leigh Shepperson)。

标签: c# class methods


【解决方案1】:

您的方法“totalViagem”不返回任何内容,您已将其返回类型设置为 double 但它什么也不返回,这就是您的问题

【讨论】:

  • 谢谢,我没有看到那个错误。我正在做所有我没有停下来寻找错误的事情。
  • 您没有得到文件和行号以及错误吗?这应该会大大缩小范围,并允许您通过单击大多数 IDE 中的错误消息来快速到达那里。
  • 不,我没有得到错误的数字。而且我使用的是 Visual Studio 2013。
猜你喜欢
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2013-07-01
  • 2014-04-16
  • 2015-02-07
  • 2012-04-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多