user1434156
user1434156

Reputation:

Working with padding and strings

I am currently working with padding in C#. I am displaying results inside a multiline textbox. The problem is this line string result1 = string.Format(format, berries + " "); giving me the error Index (zero based) must be greater than or equal to zero and less than the size of the argument list. I am not sure how to fix that. how can i display the results with even padding in between?

CODE

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

        }
    }

Upvotes: 0

Views: 153

Answers (3)

Samir Banjanovic
Samir Banjanovic

Reputation: 400

You should consider overloading ToString() of Berries and then build the entire set of Berrie values using StringBuilder. I think at the very base string.Format uses StringBuilder by overloading the ToString() and using StringBuilder it should be cleaner and more efficient.

Now that I've had more time to look at your code...I can say my last answer is not it.

What you really want to do is follow data encapsulation. You override ToString() in your base but you return an empty string. What would be better is to do your base formatting there, and then build on it from each child. I've hacked out vaguely what I am talking about. The code is below

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

}

Upvotes: 0

ZombieSheep
ZombieSheep

Reputation: 29953

The issue is that your formatted string (format) is expecting to see 5 parameters in order to format currently, but you are only supplying one input (the berries1 object).

It looks like you will need to do something like this instead.

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

See how the format string now expects 3 parameters, and that String.Format is passing three in?

Note (as per ShellShock's comment below) that you will need to modify the protection level of the num_stock, the_name and price_peritem properties in your Plants class for this to work.

Upvotes: 2

Brad Christie
Brad Christie

Reputation: 101594

You're using the following format:

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

Which implies that 5 (or more) arguments are going to be provided with String.Format() (however you're only passing 1 (and it's being cast as a string because of the + " " appended.)

Ideally, you're format call should look like:

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

Which would satisfy the format you've supplied. Alternatively, you could override the ToString method for your Berries object (but that means the format would be force-supplied by the object instead of provided for the single use of the Report method--not sure if that's desired.)

Upvotes: 1

Related Questions