我有一個重寫ToString()
方法,我要輸出格式化的數據。這些數據由11個不同的項目組成。除了一個項目外,我可以正確顯示所有項目,但該項目僅顯示爲0.不知何故,它未達到ToString()
方法。我調試的程序和隨後線,數據線,並在該點右側要去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
類由內外的ToString()
方法, - 通過實例化傳遞
amountPaid
變量在Payment
類實例化對象OrderForm
一個Payment
對象內部和ToString
方法之外,並且 - 主叫輸入
amountPaid
VA可以在toString
方法中作爲變量和屬性使用riable。
說實話,我抓住了許多吸管,我現在完全困惑,不知道該怎麼做。
你爲什麼期望它是?您可以在不帶參數的重寫方法內實例化一個'Payment'(因此AmountPaid爲0)。什麼會給你一個期望,在這裏除了0還有其他的東西? – 2012-03-05 17:10:55
我已經試過了,並且沒有使用amountPaid參數。我只是再試一次,仍然是零。當我調試它時,數據在離開「this.rtxtYourOrder.Text = orderObject.ToString();」時消失並轉到ToString()方法 – 2012-03-05 17:19:56