索引(从零开始)必须大于或等于零且小于参数列表的大小

时间:2012-03-05 01:49:25

标签: c#

尝试调用我的ToString()方法时,我收到上述错误消息。我不确定问题是什么。这是我的代码:

public override string  ToString()
{
    return string.Format("{0} Pizzas @ {1:C}: {2:C}\n" +
        "{3} Cokes @ {4:C}  {5:C}\n" +
        "Order Amount: {6:C}\n" +
        "Sales Tax: {7:C}\n" +
        "Amount Due: {8:C}\n" +
        "Amount Paid: {9:C}\n" +
        "Change Due: {10:C}", numberOfPizzas, PIZZA_PRICE + 
        totalCostOfPizza, numberOfCokes, COKE_PRICE, totalCostOfCoke +
        foodAndDrinkTotal, totalSalesTax, totalAmountDue, amountPaid, +
        changeDue);
}        

2 个答案:

答案 0 :(得分:6)

您正在以字符串格式引用更多参数,然后提供。

通过它的外观,删除所有的加号(在参数列表中不是字符串连接)并插入逗号

答案 1 :(得分:2)

看起来你正在添加你不想要的东西?

试试这个:

public override string  ToString()
{
    return string.Format("{0} Pizzas @ {1:C}: {2:C}\n" +
        "{3} Cokes @ {4:C}  {5:C}\n" +
        "Order Amount: {6:C}\n" +
        "Sales Tax: {7:C}\n" +
        "Amount Due: {8:C}\n" +
        "Amount Paid: {9:C}\n" +
        "Change Due: {10:C}", numberOfPizzas, PIZZA_PRICE,
        totalCostOfPizza, numberOfCokes, COKE_PRICE, totalCostOfCoke,
        foodAndDrinkTotal, totalSalesTax, totalAmountDue, amountPaid,
        changeDue);
}