【问题标题】:Hospital Stay: I cannot find a reason why my CalcTotalChargs is not being recognized住院:我找不到无法识别我的 CalcTotalChargs 的原因
【发布时间】:2016-03-07 00:03:58
【问题描述】:

这应该有效,因为尽管被声明为最后一个私有双精度,但 C# 的模块化应该也允许识别第一个 CalcTotalChargs。这使我无法成功运行程序

这是我目前的代码:

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;


namespace Adam_Zeidan_HW7CH6_6_Hospital_Stay
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void calculateButton_Click(object sender, EventArgs e)
        {
            *label6.Text = "You will be paying: " +         **CalcTotalChargs()**.ToString("c");*
        }

        private int CalcStayCharges()
        {
            return (350 * int.Parse(textBox1.Text));        // Calculating     the amount of days by $350
        }
        private double CalcMiscCharges()
        {
            return double.Parse(textBox2.Text) + double.Parse(textBox3.Text) +    
                double.Parse(textBox5.Text) + double.Parse(textBox5.Text);          // Adding together the other values entered within the textboxes to add to the eventual total charge
        }
        private double CalcTotalCharges()
        {
            return CalcMiscCharges() + CalcStayCharges();       // Adding     the number value of the sum of the previous calculation to the sum of the 350 *     Number of days staying
        }
    }
}

【问题讨论】:

  • 你能提供一个错误吗?就目前而言,您的代码对我来说很好,我无法重现该问题(如果有的话)。
  • 没问题!错误出现在 "label6.Text = "You will pay: " + CalcTotalChargs().ToString("c");" 行中Visual Studio C# 无法识别 CalcTotalChargs() 并说它在当前上下文中不存在。

标签: c# linq variables calculator


【解决方案1】:

您的函数拼写错误,因此无法完成。

CalcTotalChargs().ToString("c") 应该是CalcTotalCharges().ToString("c")

使用下面的代码应该可以解决问题。

    private void calculateButton_Click(object sender, EventArgs e)
    {
        label6.Text = "You will be paying: " +         CalcTotalCharges().ToString("c");
    }

【讨论】:

  • 应该怎么拼写?我不认为这会是问题。
  • 相信我。您在方法调用中忘记了e,因此方法CalcTotalChargs()在当前上下文中不存在”。您不能调用不存在的方法。
  • 哈哈!我想这是对我来说很简单的事情。我以为是技术问题。该代码现在工作正常。感谢您的帮助,Gabe!
  • 没问题 - 很高兴我帮忙
猜你喜欢
  • 2022-11-24
  • 2021-12-16
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-06
  • 1970-01-01
相关资源
最近更新 更多