gpt4 book ai didi

c# - 如何从 C# (ASP.NET) 中的函数返回多个值?

转载 作者:行者123 更新时间:2023-11-30 19:33:22 31 4
gpt4 key购买 nike

我有一个页面,其中包含一些产品和用户输入数字的文本字段。我首先使用 JavaScript 来计算总成本。根据他们输入的用户数量,他们会得到不同的费率(如下面的代码所示)。当用户在文本字段中键入或粘贴数字时,会调用 CalculateCost 函数,该函数会调用其他函数(示例中仅显示两个 CDCOst 和 DVDCost),以确保字段 Monthly Cost 和 Annual Cost 显示正确的值。

在插入数据库之前,我当然也想在后面的代码中做最后的计算。我怎样才能在 C# 中实现类似的东西?

function CDCost() {
var monthlyAmount;
var annualAmount;
var amount;
var users = $('#txtCD').val();

if (users > 0 && users < 100) {
amount = users * 14.95;
monthlyAmount = amount;
annualAmount = monthlyAmount * 12;
return [monthlyAmount, annualAmount];
}
if (users >= 100 && users <= 250) {
amount = users * 12.95;
monthlyAmount = amount;
annualAmount = monthlyAmount * 12;
return [monthlyAmount, annualAmount];
}
if (users == 0) {
monthlyAmount = 0;
annualAmount = 0;
return [monthlyAmount, annualAmount];
}
}

function DVDCost() {
var monthlyAmount;
var annualAmount;
var amount;
var users = $('#txtDVD').val();

if (users > 0 && users < 100) {
amount = users * 16.95;
monthlyAmount = amount;
annualAmount = monthlyAmount * 12;
return [monthlyAmount, annualAmount];
}
if (users >= 100 && users <= 250) {
amount = users * 14.95;
monthlyAmount = amount;
annualAmount = monthlyAmount * 12;
return [monthlyAmount, annualAmount];
}
if (users == 0) {
monthlyAmount = 0;
annualAmount = 0;
return [monthlyAmount, annualAmount];
}
}


function CalculateCost() {
var cd = CDCost();
var dvd = DVDCost();

var monthlyCost = cd[0] + dvd[0];
var annualCost = cd[1] + dvd[1];

return [monthlyCost, annualCost];
}

$('#txtCD').bind('keyup change', function (ev) {
var cost = CalculateCost();
var monthly = cost[0];
var annual = cost[1];

$('#MonthlyCostSum').text(monthly);
$('#AnnualCostSum').text(annual)
});

我如何在 C# 中继续这样做?

类似于:

protected double CDCost()
{
double monthlyAmount;
double annualAmount;
double amount;
double users = Convert.ToDouble(txtCD.Text);

if (users > 0 && users < 100)
{
amount = users * 14.95;
monthlyAmount = amount;
annualAmount = monthlyAmount * 12;
return //how do I return the values here to a CalculateCost function?
}


}

最佳答案

要么使用 out 参数,要么创建一个新类型来包装所有你想返回的东西(所谓的“属性包”):

class ReportData
{
public double MonthlyAmount { get; set; }
public double AnnualAmount { get; set; }
public double Amount { get; set; }
}

...

protected ReportData CDCost()
{
return new ReportData()
{
Amount = users * 14.95
MonthlyAmount = amount,
AnnualAmount = amount * 12.0,
};
}

关于c# - 如何从 C# (ASP.NET) 中的函数返回多个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3845831/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com