0

我目前正在使用 C# 中的填充。我在多行文本框中显示结果。问题是这条线string result1 = string.Format(format, berries + " ");给了我错误Index (zero based) must be greater than or equal to zero and less than the size of the argument list。我不知道如何解决这个问题。如何在中间填充均匀的情况下显示结果?

代码

 namespace farm
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            public abstract class Plants
            {
                protected string the_name;
                protected double num_stock;
                protected double price_peritem;
                protected double total_item_value;

                public Plants(string new_name, int new_stock, double new_price)
                {
                    the_name = new_name;
                    num_stock = new_stock;
                    price_peritem = new_price;
                }

                public override string ToString()
                {
                    return "";
                }

                public virtual double Get_Value()
                {
                    double s = 0;
                    return s;
                }


            }
     public class Berries : Plants
            {
                string variety;
                string months;

                public Berries(string new_name, int new_stock, double new_price, string new_variety, string new_months)
                    : base(new_name, new_stock, new_price)
                {
                    variety = new_variety;
                    months = new_months;

                    total_item_value = num_stock * price_peritem;

                    //total_value += total_item_value;

                }

                public override string ToString()
                {
                    string s = "Berries" + "     " + num_stock + "      " + the_name + "     " + price_peritem;
                    return s;
                }

                public override double Get_Value()
                {

                    total_item_value = num_stock * price_peritem;
                    return total_item_value;
                }
            }

    public void Report()
            {
                const string format = "{0,-25} {1,-25} {2,-25} {3,-25} {4,-25}";



                Berries berries1 = new Berries("BlueBerries", 12, 5, "AAA Early", "July");
                string result1 = string.Format(format, berries1 + " ");
                textBox1.AppendText(result1 + Environment.NewLine);



                Berries berries2 = new Berries("Strawberry", 12, 5, "FrostStar", "December");
                string result = string.Format(format, berries2 + " ");
                textBox1.AppendText(result + Environment.NewLine);


            }

    private void button1_Click(object sender, EventArgs e)
            {
                Report();
            }

        }
    }
4

3 回答 3

3

问题是您的格式化字符串(格式)希望看到 5 个参数以便当前格式化,但您只提供一个输入(berries1 对象)。

看起来你需要做这样的事情。

const string format = "{0,-25} {1,-25} {2,-25}";
String.Format(format, berries1.num_stock, berries1.the_name, berries1.price_peritem);

看看格式字符串现在需要 3 个参数,而 String.Format 传递了三个参数?

请注意(根据下面 ShellShock 的评论),您需要修改类中的num_stock,the_nameprice_peritem属性的保护级别Plants才能使其正常工作。

于 2012-10-17T14:15:29.657 回答
1

您正在使用以下格式:

"{0,-25} {1,-25} {2,-25} {3,-25} {4,-25}"

这意味着将提供 5 个(或更多)参数String.Format()(但是您只传递 1 个(并且由于+ " "附加了它被转换为字符串。)

理想情况下,您的格式调用应如下所示:

String.Format(format, berries1.prop1, berries1.prop2,
                      berries1.prop3, berries1.prop4,
                      berries1.prop5);

这将满足您提供的格式。或者,您可以覆盖对象的ToString方法Berries(但这意味着格式将由对象强制提供,而不是为Report方法的单一使用提供 - 不确定是否需要。)

于 2012-10-17T14:16:59.780 回答
1

您应该考虑对 Berries 进行重载ToString(),然后使用 构建整个 Berrie 值集StringBuilder。我认为最基础的 string.FormatStringBuilder通过重载ToString() 使用StringBuilder它应该更清洁、更高效。

现在我有更多时间查看您的代码......我可以说我的最后一个答案不是。

你真正想做的是遵循数据封装。你在你的基础上覆盖ToString(),但你返回一个空字符串。更好的是在那里做你的基本格式,然后从每个孩子开始。我已经模糊地破解了我在说什么。代码如下

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public abstract class Plants
    {                                   
        private string the_name;
        private double num_stock;
        private double price_peritem;
        private double total_item_value;

        public string The_Name
        {
            get
            {
                return the_name;
            }
            protected set
            {
                the_name = value;
            }
        }

        public double Num_Stock
        {
            get
            {
                return num_stock;
            }
            protected set
            {
                num_stock = value;
            }
        }

        public double Price_PerItem
        {
            get
            {
                return price_peritem;
            }
            protected set
            {
                price_peritem = value;
            }
        }

        public double Total_Item_Value
        {
            get
            {
                return Num_Stock * Price_PerItem;
            }               
        }


        public Plants(string new_name, int new_stock, double new_price)
        {
            The_Name = new_name;
            Num_Stock = new_stock;
            Price_PerItem = new_price;
        }

        public override string ToString()
        {
            return string.Format("{0} {1,-25} {2,-25} {3,-25}", The_Name, Num_Stock, Price_PerItem, Total_Item_Value);
        }

        public virtual double Get_Value()
        {
            double s = 0;
            return s;
        }
    }


    public class Berries : Plants
    {
        private string variety;
        private string months;

        public string Variety
        {
            get
            {
                return variety;
            }
            protected set
            {
                variety = value;
            }
        }

        public string Months
        {
            get
            {
                return months;
            }
            protected set
            {
                months = value;
            }
        }

        public Berries(string new_name, int new_stock, double new_price, string new_variety, string new_months)
            : base(new_name, new_stock, new_price)
        {
            Variety = new_variety;
            Months = new_months;                
        }

        public override string ToString()
        {
            return string.Format(base.ToString() + " {0} {1}", Variety, Months);
        }


        //This is now replaced by simply using the TotalCost property
        //public override double Get_Value()
        //{
        //    return TotalCost;                
        //}
    }

    public void Report()
    {
        //const string format = "{0,-25} {1,-25} {2,-25} {3,-25} {4,-25}";

        Berries berries1 = new Berries("BlueBerries", 12, 5, "AAA Early", "July");
        //now you can modify the result1 string to 
        string result1 = berries1.ToString(); //string.Format(format, berries1 + " ");
        textBox1.AppendText(result1 + Environment.NewLine);



        Berries berries2 = new Berries("Strawberry", 12, 5, "FrostStar", "December");
        string result = berries1.ToString(); //string.Format(format, berries2 + " ");
        textBox1.AppendText(result + Environment.NewLine);


    }

    private void button1_Click(object sender, EventArgs e)
    {
        Report();
    }

}
于 2012-10-17T14:22:03.763 回答