2015-10-30 28 views
-1

我已經被賦予了一個任務,用不同的方法計算醫院費用。我已經想出了大部分,但我被困在一個部分。當我嘗試從另一個方法中使用變量時,該值似乎沒有移到新方法中。什麼是正確的方式去做這件事?我遇到了CalcTotalCharges方法的問題。C#問題與使用方法

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; 
     } 
    } 
} 
+0

時要調用返回一個數據類型,例如'int'執行以下'VAR someClacStayCharge = CalcStayCharge(dayStayd)的方法;'例如做對其他2層的方法相同..你正在返回的INT,但從來沒有真正捕獲/分配給一個局部變量在'enter_Click'事件的範圍內使用,如果你希望你也可以替換局部變量並將它們轉換爲自動屬性並將int值存儲在那裏..有幾種方法可以處理這個問題..但是現在你所做的只是調用一個方法並返回一些永遠不會被捕獲的東西 – MethodMan

+0

@Ben很明顯看到什麼方法正在工作,但是OP期望如何調用該方法不起作用 'CalcTotalCharges(totalF EE,stayCost); total.Text = totalCost.ToString();' – MethodMan

+0

@TacosaurusRex我建議你對下面的'無效方法和返回值的方法'進行一次谷歌搜索,這將有助於你理解如何調用方法返回一個值來捕獲和分配它們的值[MSDN返回C#參考](https://msdn.microsoft.com/en-us/library/1h3swy84.aspx) – MethodMan

回答

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(); 
} 
+0

我立即看到,但想給OP有機會思考如何將返回值分配給變量+1 – MethodMan