Reputation: 1227
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:
amountPaid
variable in the OrderFrom
class by instantiating an OrderForm
object both inside and outside of the ToString()
method,amountPaid
variable in the Payment
class by instantiating a Payment
object both inside and outside of the ToString
method, andamountPaid
variable in the toString
method 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.
Upvotes: 3
Views: 188
Reputation: 37543
The first thing that I see is that your architecture is off. You have a PizzaOrder
as a "sort of" member of the Payment
class, and your ToString()
method is a member of (I assume) the PizzaOrder
class. Rather than building formatting methods to handle all of this, you should relate your objects appropriately.
A pizza order should have a payment associated with it so you should have a class structure similar to:
public class PizzaOrder
{
...
public Payment PaymentInfo { get; set; }
// then have your method:
// What ToString() method are you overriding?
// Does the baseclass for a pizza order already
// have a ToString()?
public override string ToString()
{
double paymentAmount = 0;
if (this.PaymentInfo != null)
paymentAmount = this.PaymentInfo.AmountPaid;
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, paymentAmount,
GetChangeDue(totalAmountDue, paymentAmount));
}
}
I think you need to revisit your architecture a little more thoroughly and establish stronger relationships between your classes.
Upvotes: 0
Reputation: 726839
Your ToString
provides wrong payment info because it, quite honestly, does not have access to that payment object that you are creating. Instantiating new Payment
does not help: you need a different approach here.
One way to deal with the problem is to make a FormatWithPayment
method, instead of overriding ToString
. It is a good idea to avoid using the plain ToString
in your business code, reserving it to debugging and logging.
public string FormatWithPayment(Payment paymentOrder )
{
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)
);
}
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:
this.rtxtYourOrder.Text = orderObject.FormatWithPayment(orderPaymentObject);
Upvotes: 1
Reputation: 43056
Your sample code shows the ToString()
override outside the Payment class. If that's a true representation of your actual code, then you have overridden ToString()
on some class other than Payment
. This analysis is supported by your instantiation of a new Payment
object inside the method.
The ToString()
method is an instance method. It should return a string representation of the instance on which it is called. If you're trying to get a string representation of a Payment
, the method should be an instance method of the Payment
class, and it should get its values from the instance properties (and possibly instance fields) of that class.
Make the method a member of the Payment
class, and use this.
rather than paymentOrder.
; this should solve the problem.
Upvotes: 2