Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7593489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:10:55+00:00 2026-05-30T21:10:55+00:00

I have an overridden ToString() method that I want to output formatted data. This

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:10:56+00:00Added an answer on May 30, 2026 at 9:10 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WeekdayException class that has the ToString(). I want to return the
I have an array of different objects with different data types I want to
I have implemented a ToString() override method for my class in my Webservice and
I have a class MyClass, and I would like to override the method ToString()
I have subclassed java.awt.Frame and have overridden the paint() method as I wish to
Let's say that you have overridden an object's equals() and hashCode() methods, so that
I have a simple class that essentially just holds some values. I have overridden
Problem: I have a method that creates a list from the parsed ArrayList. I
HI I have a working SmsReceiver and a method that will search to contacts
Let's say I have a class like this (and also further assume that all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.