数据未达到重写ToString()方法

时间:2012-03-05 17:07:22

标签: c#

我有一个重写的ToString()方法,我想输出格式化的数据。该数据由11个不同的项目组成。我可以得到除了一个项目以外的所有项目,但是一个项目只显示为0.不知何故,它没有达到ToString()方法。我调试了程序并逐行跟踪数据,它在转到ToString()方法之前就消失了,我不明白为什么。这是我的代码。我只发布了我认为涉及传递数据的代码。如果我错了并且需要所有代码,请告诉我。

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));
    }        

未传递的值是amountPaid(距离最后一个)。

我试过了:

  • 通过在amountPaid方法的内部和外部实例化OrderFrom对象,在OrderForm类中传递ToString()变量,
  • 通过在amountPaid方法的内部和外部实例化Payment对象来传递Payment类中的ToString变量,
  • 调用amountPaid方法中的toString变量作为变量和属性输入。

说实话,我已经掌握了这么多吸管,现在我感到很困惑,不知道该怎么办。

3 个答案:

答案 0 :(得分:2)

您的示例代码显示了{class 1}}覆盖之外的付款类。如果这是您实际代码的真实表示,那么您已在ToString()以外的某些类上覆盖了ToString()。您在方法中实例化Payment对象支持此分析。

new Payment方法是一种实例方法。它应返回调用它的实例的字符串表示形式如果您尝试获取ToString()的字符串表示形式,则该方法应该是{的实例方法{1}}类,它应该从该类的实例属性(可能还有实例字段)中获取它的值。

使该方法成为Payment类的成员,并使用Payment而不是Payment;这应该可以解决问题。

答案 1 :(得分:1)

您的ToString提供了错误的付款信息,因为它实际上无法访问您正在创建的付款对象。实例化new Payment无济于事:您需要采用不同的方法。

解决问题的一种方法是制作FormatWithPayment方法,而不是覆盖ToString。最好避免在业务代码中使用普通ToString,并将其保留用于调试和记录。

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)
        );
}

它与您的代码几乎相同,现在只传入付款对象。

现在,您可以修改点击处理程序以使用此新方法,如下所示:

this.rtxtYourOrder.Text =  orderObject.FormatWithPayment(orderPaymentObject);

答案 2 :(得分:0)

我看到的第一件事是您的架构已关闭。您有PizzaOrder作为Payment类的“某种”成员,而您的ToString()方法是(我假设)PizzaOrder类的成员。您应该适当地关联对象,而不是构建格式化方法来处理所有这些。

披萨订单应该有与之相关的付款,因此您应该具有类似于以下的类结构:

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));
    }     

}

我认为您需要更彻底地重新审视您的架构,并在您的班级之间建立更牢固的关系。