I have an overridden ToString() method that I want to output formatted data. This data consisted of 11 different items. I can get all but one item to display properly but that one item just shows up as 0. Somehow, it is not reaching the ToString() method. I debugged the program and followed the data line by line and it disappears at the point right before going to the ToString() method and I have no idea why. Here is my code. I’m only posting the code I think is involved with passing the data. If I am wrong and all of the code is needed, let me know.
private void btnPaymentButton_Click(object sender, EventArgs e)
{
amountPaid = double.Parse(this.txtAmountPaid.Text);
orderPaymentObject = new Payment(orderObject.TotalAmountDue, amountPaid);
this.txtNumberOfPizzaOrdered.Clear();
this.txtNumberOfCokesOrdered.Clear();
this.txtAmountDue.Clear();
this.txtAmountPaid.Clear();
this.lblYourOrder.Visible = true;
this.rtxtYourOrder.Visible = true;
this.rtxtYourOrder.Text = orderObject.ToString();
}
.......
public class Payment
{
PizzaOrder orderObject;
double amountPaid = 0.0,
totalAmountDue = 0.0;
public Payment()
{
}
public Payment(double amountDue, double payment)
{
orderObject = new PizzaOrder();
amountPaid = payment;
totalAmountDue = amountDue;
orderObject.GetChangeDue(totalAmountDue, amountPaid);
//orderObject.ToString();
}
public Payment(double payment)
{
amountPaid = payment;
}
public double AmountPaid
{
get
{
return this.amountPaid;
}
}
}
......
public override string ToString()
{
Payment paymentOrder = new Payment();
return string.Format(" {0} Pizzas @ {1:C}: {2,8:C}\n" +
" {3} Cokes @ {4:C}: {5,8:C}\n" +
" Order Amount: {6,8:C}\n" +
" Sales Tax: {7,9:C}\n" +
" Amount Due: {8,8:C}\n" +
" Amount Paid: {9,9:C}\n\n" +
" Change Due: {10,9:C}", NumberOfPizzas,
PIZZA_PRICE, totalCostOfPizza, NumberOfCokes, COKE_PRICE,
totalCostOfCoke, FoodAndDrinkTotal, TotalSalesTax,
TotalAmountDue, paymentOrder.AmountPaid, GetChangeDue(totalAmountDue,amountPaid));
}
The value that is not being passed is the amountPaid (second from last).
I’ve tried:
- passing the
amountPaidvariable in theOrderFromclass by instantiating anOrderFormobject both inside and outside of theToString()method, - passing the
amountPaidvariable in thePaymentclass by instantiating aPaymentobject both inside and outside of theToStringmethod, and - calling inputting the
amountPaidvariable in thetoStringmethod as a variable and as a property.
To be honest I’ve grasped at so many straws that I am now completely confused and have no idea what to do.
Your
ToStringprovides wrong payment info because it, quite honestly, does not have access to that payment object that you are creating. Instantiatingnew Paymentdoes not help: you need a different approach here.One way to deal with the problem is to make a
FormatWithPaymentmethod, instead of overridingToString. It is a good idea to avoid using the plainToStringin your business code, reserving it to debugging and logging.It’s almost the same as your code, only the payment object is now passed in.
Now you can modify your click handler to use this new method, like this: