C#使用方法的问题

时间:2015-10-30 00:54:41

标签: c#

我接受了以不同方法计算医院费用的任务。我已经想出了大部分内容,但我被困在一个方面。当我尝试使用来自另一个方法的变量时,该值似乎不会转移到新方法。怎样才能解决这个问题呢?我有CalcTotalCharges方法的问题。

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 hospitalBills
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {

        }
        private void enter_Click(object sender, EventArgs e)
        {
            int dayStayd = int.Parse(dayStay.Text);
            int medFee = int.Parse(medCharge.Text);
            int surgFee = int.Parse(surgCharges.Text);
            int labFee = int.Parse(labCharges.Text);
            int rhbFee = int.Parse(rhbCharges.Text);
            CalcStayCharge(dayStayd);
            CalcMiscCharges(medFee, surgFee, labFee, rhbFee);
            CalcTotalCharges(totalFee,stayCost);
            total.Text = totalCost.ToString();

        }
        public int CalcStayCharge(int dayStayd)
        {
            int stayCost = dayStayd * 350;
            return stayCost;
        }
        public int CalcMiscCharges(int medFee, int surgFee, int labFee, int rhbFee)
        {
            int totalFee = medFee + surgFee + labFee + rhbFee;
            return totalFee;
        }
        public int CalcTotalCharges(int totalFee, int stayCost)
        {
            int totalCost = totalFee + stayCost;
            return totalCost;
        }
    }
}

1 个答案:

答案 0 :(得分:3)

正如@MethodMan在他的评论中指出的那样,你的职能和工作是"但是您需要捕获变量中的输出才能使用它们。请参阅下面的示例,了解如何执行此操作。

private void enter_Click(object sender, EventArgs e)
{
    int dayStayd = int.Parse(dayStay.Text);
    int medFee = int.Parse(medCharge.Text);
    int surgFee = int.Parse(surgCharges.Text);
    int labFee = int.Parse(labCharges.Text);
    int rhbFee = int.Parse(rhbCharges.Text);
    var stayCost = CalcStayCharge(dayStayd);
    var totalFee = CalcMiscCharges(medFee, surgFee, labFee, rhbFee);
    var totalCost = CalcTotalCharges(totalFee,stayCost);
    total.Text = totalCost.ToString();
}