Reputation: 476
Let's say I have the following code:
protected void ReservationForm()
{
double x = 50;
double tva = 1.196;
double totalAmount = 0;
totalAmount = x * tva;
ltrl_showText = "Total Amount is " + totalAmount;
}
protected void btn_submit_Click(object sender, EventArgs e)
{
ltrl_previewText = "You ordered nameProduct at the price of " + totalAmount ;
}
How can I get the variable totalAmount from the function above?
I know that with Java I can do this with super()
and I found that the equal property on C# is base
but I don't know how to use it. I tried to change the function to public instead of protected but I always get an error.
Thanks for your time.
EDIT: I use AJAX, that's why the total Amounts are on both functions. The first one I need it because the total amount changes depending the user selection and then on click he sees the preview page.
Upvotes: 0
Views: 18053
Reputation: 532465
Rather than pretend I can solve your problem, which I don't really feel that I have enough information to do, I'll share with you a general principle: DRY or Don't Repeat Yourself. In your case, you have a piece of code that you want to use in two places. So far, you're doing well in that the calculation only takes place in one method. That's good and in keeping with the DRY principle.
Now some people would be tempted to just cut/paste the code between the two methods. This would violate the DRY principle, because you'd have the same code repeated and have to maintain it in two places. Worse yet, they could diverge accidentally and start to do different things leading to bugs. You've avoided that trap by wanting to use the code in the other method directly. So far, so good.
The next step is refactoring to take advantage of the existing code. This is where it can get tricky because you typically only want to re-use just a part of the code. I suspect in your case that you don't always want to set ltrl_showText
whenever you set ltrl_previewText
. If that's the case, then you should introduce a third method that each of the first two call that does the calculation for both.
protected decimal CalculateAmount()
{
decimal x = 50;
decimal tva = 1.196;
decimal totalAmount = 0;
return x * tva;
}
protected void ReservationForm()
{
decimal totalAmount = CalculateAmount();
ltrl_showText = "Total Amount is " + totalAmount;
}
protected void btn_submit_Click(object sender, EventArgs e)
{
decimal totalAmount = CalculateAmount();
ltrl_previewText = "You ordered nameProduct at the price of " + totalAmount ;
}
Now, you might think we're done, but I don't find this very useful. The totalAmount
is always the same. That doesn't seem very useful. At this point, though, I have to enter the realm of pure conjecture. Clearly, the values used for the function come from somewhere other than constants in the code, at least the price must if not the tax rate. The question is do these come from the same place for both ReservationForm
and the button click handler. I suspect not, so the most reasonable approach would be to make CalculateAmount
take one or more parameters.
public decimal CalculateAmount( double cost )
{
decimal tva = 1.196;
return cost * tva;
}
protected void ReservationForm()
{
decimal cost = ...where do we get cost from? a database, a previous form, ???
decimal totalAmount = CalculateAmount(cost);
ltrl_showText = "Total Amount is " + totalAmount;
}
protected void btn_submit_Click(object sender, EventArgs e)
{
decimal cost = double.Parse( inp_ProductOrdered.Text ); // seems reasonable on a button click
decimal totalAmount = CalculateAmount(cost);
ltrl_previewText = "You ordered nameProduct at the price of " + totalAmount ;
}
Now the only thing that still bothers me is where does the tax rate come from? It's likely that it depends on where the person is ordering from. In that case, you probably need to introduce a fourth method that does some sort of look up based on the location of the person doing the ordering and returns the appropriate tax rate for that person. I'll leave that as an exercise.
I have no idea if this solves your problem, but this is how I would go about doing the refactoring. Think about what you can share and where those things derive their data. Abstract that to the most re-usable code that you can.
EDIT: You really should be using decimal
(or a custom Money type) rather than double
for money, cf., What is the best data type to use for money in c#?
Upvotes: 2
Reputation: 17427
try making totalAmount
a private field:
class foo {
private double totalAmount;
protected void ReservationForm()
{
double x = 50;
double tva = 1.196;
totalAmount = x * tva;
ltrl_showText = "Total Amount is " + totalAmount;
}
protected void btn_submit_Click(object sender, EventArgs e)
{
ltrl_previewText = "You ordered nameProduct at the price of " + totalAmount ;
}
}
Upvotes: 0
Reputation: 44326
Use a private field:
private double totalAmount;
protected void ReservationForm()
{
double x = 50;
double tva = 1.196;
this.totalAmount = x * tva;
ltrl_showText = "Total Amount is " + this.totalAmount;
}
protected void btn_submit_Click(object sender, EventArgs e)
{
ltrl_previewText = "You ordered nameProduct at the price of " + this.totalAmount ;
}
Upvotes: 2
Reputation: 20620
You could re-arrange your code like this:
protected double ReservationForm()
{
double x = 50;
double tva = 1.196;
double totalAmount = 0;
totalAmount = x * tva;
return totalAmount;
}
protected void btn_submit_Click(object sender, EventArgs e)
{
double TotalAmount;
TotalAmount = ReservationForm();
ltrl_previewText = "You ordered nameProduct at the price of " + TotalAmount;
ltrl_showText = "Total Amount is " + TotalAmount;
}
Upvotes: 1
Reputation: 6425
If ReservationForm() is in the code behind you could assign the value to a protected property (which is available to the asp.net design part of the page), eg:
protected string reservationFormTotalAmount;
protected void ReservationForm()
{
double x = 50;
double tva = 1.196;
double totalAmount = 0;
totalAmount = x * tva;
reservationFormTotalAmount = "Total Amount is " + totalAmount;
}
and in the UI
ltrl_previewText = reservationFormTotalAmount;
Upvotes: 0
Reputation: 7546
totalAmount
is a local variable. This means it can only be accessed from within its method.
You could make it a field instead, and then it would have a wider scope.
protected double totalAmount;
protected void ReservationForm()
{
double x = 50;
double tva = 1.196;
totalAmount = x * tva;
ltrl_showText = "Total Amount is " + totalAmount;
}
Of course you will want to make sure ReservationForm()
is called before btn_submit_Click()
.
Upvotes: 0