Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8691331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:06:16+00:00 2026-06-13T00:06:16+00:00

I am currently working with padding in C#. I am displaying results inside a

  • 0

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

        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:06:17+00:00Added an answer on June 13, 2026 at 12:06 am

    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();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working with a textbox that has a background. I was wondering if
This is the code I am working with currently: http://jsfiddle.net/HMsKa/39/ HTML <!DOCTYPE HTML PUBLIC
I'm new to Asp.net and I'm currently working with GridViews. I've looked around this
I'm currently working on a site http://www.lovepotatoes.ovh3.microserveltd.co.uk/ and this question is in relation to
I am currently working with a customized jquery alert from this SITE . I
Currently working with converting SQLException error messages into messages that are more useful for
Currently working with the following package structure: /package __init__.py final.py /write __init__.py write.py /data
Currently working for the first time with JSON and with little experience of jQuery.
Currently working in the deployment of an OFBiz based ERP, we've come to the
Currently working on a VBScript to automate some of the dirty PST ingestion work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.