I have a page where some products and textfields where the user enters a number. I first use JavaScript to calculate the total cost. Based on how many users they enter they get a different rate (as shown in code below). When the user types or paste a number into a textfield the function CalculateCost gets called which calls the other functions (only showing two in the example, CDCOst and DVDCost) to make sure the fields Monthly Cost and Annual Cost are displaying the correct value.
I of course want to do the final calculation in the code behind as well before I insert into the database. How can I achieve something similiar in 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)
});
How would I go on doing this in C#?
Something like:
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?
}
}
Either use out parameters or create a new type wrapping all the things you would like to return (a so-called "property bag"):
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,
};
}
The tuple class in .NET 4 does the job perfectly.
protected Tuple<double, double, double, 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?
}
return new Tuple<double, double, double, double>(monthlyAmount, annualAmount, amount, users);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With