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 つのパラメーターを期待しているのに、1 つの入力 (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 が 3 つを渡していることがわかりますか?

(以下のShellShockのコメントに従って)これが機能するには、クラスの保護レベルを変更する必要があることに注意num_stockしてください。the_nameprice_peritemPlants

于 2012-10-17T14:15:29.657 に答える
1

Berries のオーバーロードを検討ToString()してから、 を使用して Berrie 値のセット全体を構築する必要がありますStringBuilder。私は非常に基本的な string.Formatで() をStringBuilderオーバーロードして使用すると思います。それを使用すると、よりクリーンで効率的なはずです。ToStringStringBuilder

あなたのコードを見る時間が増えたので...私の最後の答えはそうではないと言えます。

本当にやりたいことは、データのカプセル化に従うことです。ベースでオーバーライド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 に答える
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(ただし、これは、メソッドを 1 回使用するために提供されるのではなく、オブジェクトによって形式が強制的に提供されることを意味しますReport。それが望ましいかどうかはわかりません)。

于 2012-10-17T14:16:59.780 に答える