如何将值从一种方法传递到另一种方法?

我正在使用几种私人方法开发员工薪水计算器。 这些方法将确定加class时间和常规时间。 我还必须创建定期支付和加class费的方法。 我的问题是,我可以将小时方法的结果输入确定薪酬的方法吗? 如果可能的话,它会如何运作? 有问题的方法是CalculateBasePayAmount – 我可以将另一个方法的结果传递到这里吗?

这是我到目前为止所看到的内容。 感谢任何人都能提供的帮助!

public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //Determine Hours Method private decimal DetermineBasePayHours(decimal parhoursWorked) { decimal baseHours = 0; decimal overtimeHours = 0; if (parhoursWorked  40) { overtimeHours = parhoursWorked - 40; baseHours = parhoursWorked - overtimeHours ; } return baseHours; } private decimal DetermineOverTimeHours(decimal parHoursWorked, string parCategory) { decimal overtimeHours = 0; if (parHoursWorked > 40 && parCategory!="MGR") { overtimeHours = parHoursWorked - 40; } return overtimeHours; } private decimal CalculateBasePayAmount(decimal basePayHours, string parCategory) { decimal basePay = 0; decimal mgrWage = 20; decimal salesWage = 15; decimal staffWage = 10; basePayHours= DetermineBasePayHours(basePayHours); if(parCategory == "MGR" && basePayHours > 40) { basePay = 40 * mgrWage; } else { basePay = basePayHours * mgrWage; } if (parCategory =="SR") { basePay = basePayHours * salesWage; } else if (parCategory == "STF") { basePay = basePayHours * staffWage; } return basePay; } protected void butCalcPay_Click(object sender, EventArgs e) { ////1. Declare Variables //decimal mgrWage = 20; //decimal salesWage = 15; //decimal staffWage = 10; //decimal basePay = 0M; //decimal salesOvertime = 22.50M; //decimal staffOvertime = 15; //decimal overtimeHours = 0; //decimal overtimePay = 0; //decimal totalPay = 0; decimal totalHours = 0; decimal bpHours; decimal otHours; string empCat; decimal basePay; //2. Get Values for Variables totalHours = Convert.ToDecimal(txtNumberHours.Text); empCat = Convert.ToString(ddlCategory.SelectedValue); // 3. Methods Called bpHours = DetermineBasePayHours(totalHours); otHours = DetermineOverTimeHours(totalHours, empCat); basePay = CalculateBasePayAmount(totalHours, empCat); // 4. Display Results lblbasePay.Text = "Base Pay " + basePay.ToString("C");e here 

我可以将小时方法的结果输入确定薪酬的方法吗?

以某种方式说,是的。 虽然我认为混淆来自你描述它的方式和你使用的术语。

在这种情况下,我不完全清楚你在寻找什么具体的值,但看起来你的方法基本上只是接受一些值,运行一些计算,并返回一些值。 任何调用这些函数的代码都将获得这些返回的值,并可以使用它们来调用其他函数。 作为一个人为的例子:

 private int Method1(int someValue) { // perform some calculation, then... return anotherValue; } private int Method2(int someValue) { // perform some calculation, then... return anotherValue; } 

然后消耗代码将能够使用这些函数执行更大的计算:

 var calculatedValue = Method1(5); var furtherCalculatedValue = Method2(calculatedValue); 

在第一函数的结果随后被用作第二函数的输入的意义上,这基本上将第一函数的“结果”馈送到第二函数中。 这些函数彼此之间没有任何相关知识,它们不“相互提供数据”,在这种情况下,它们只是根据计算返回值。 使用代码可以选择将一个函数的结果用作另一个函数的参数。

您需要阅读有关编程的更多内容以及我们如何/为什么我们拥有返回上述值的方法。 使用上面的代码,您将希望使用这样的值:

 // basePayHours is the decimal amount returned by DetermineBasePayHours. var basePayHours = DetermineBasePayHours(parhoursWorked); // overTimeHours is the decimal amount returned by DetermineOverTimeHours. var overTimeHours = DetermineOverTimeHours(parHoursWorked, parCategory); // basePayAmount is the decimal amount returned by CalculateBasePayAmount. var basePayAmount = CalculateBasePayAmount(basePayHours, parCategory);