我目前正在使用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
。我不知道如何解决这个问题。如何在两者之间均匀填充显示结果?
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();
}
}
}
答案 0 :(得分: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_name
,price_peritem
和Plants
属性的保护级别工作。
答案 1 :(得分:1)
您使用以下格式:
"{0,-25} {1,-25} {2,-25} {3,-25} {4,-25}"
这意味着将向String.Format()
提供5个(或更多)参数(但是你只传递1(并且由于附加了+ " "
而将其转换为字符串。)
理想情况下,您的格式调用应如下所示:
String.Format(format, berries1.prop1, berries1.prop2,
berries1.prop3, berries1.prop4,
berries1.prop5);
哪个会满足您提供的格式。或者,您可以覆盖ToString
对象的Berries
方法(但这意味着格式将由对象强制提供,而不是仅为单次使用Report
方法提供 - - 不确定是否需要。)
答案 2 :(得分:1)
您应该考虑重载ToString()
Berries,然后使用StringBuilder
构建整个Berrie值集。我认为在最基础的字符串.Format使用StringBuilder
重载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();
}
}