Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data not reaching overridden ToString() method

Tags:

c#

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 amountPaid variable in the OrderFrom class by instantiating an OrderForm object both inside and outside of the ToString() method,
  • passing the amountPaid variable in the Payment class by instantiating a Payment object both inside and outside of the ToString method, and
  • calling inputting the amountPaid 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.

like image 472
Programming Newbie Avatar asked Aug 07 '26 02:08

Programming Newbie


2 Answers

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.

like image 138
phoog Avatar answered Aug 09 '26 15:08

phoog


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);
like image 26
Sergey Kalinichenko Avatar answered Aug 09 '26 15:08

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!