【发布时间】:2011-03-13 07:39:54
【问题描述】:
我是一个 C# 新手,我真的很困惑我要为 C# 类中的项目做些什么。
赋值是 C# 中的一些列表操作。
程序接受文本框中的项目列表,然后遍历这些项目,创建列表的多个副本。它将列表的每个副本随机调整为 3 和所有项目之间的大小。然后它会输出所有副本。
我遇到的问题是,当我使用调试器单步执行该程序时,我得到了预期的输出。如果我在每次迭代后显示一个消息框,也会发生同样的情况(如下面的代码所示)。
但是,如果我直接运行程序,我会得到不同的输出。列表的所有副本都完全相同,而不是列表的变化。
如果您在代码中看到我已注释“// FIRST DEBUG MESSAGEBOX”和“// SECOND DEBUG MESSAGEBOX”。如果第一个调试消息框代码保留在那里,则输出与预期一样...输出列表的多个版本,长度介于 3 和所有项目之间。
但是,这就是我感到困惑的地方...如果您注释掉第一个调试消息框代码,您会得到不同的结果。所有版本的列表输出长度相同,没有变化。
任何帮助将不胜感激!这是我到目前为止的代码......如果它很糟糕很抱歉 - 我是 C# 的新手:
public partial class MainForm : Form
{
/**
* Vars to hold raw text list items
* and list items split by line
*/
String rawListItems = "";
List<string> listItems = new List<string>();
List<List<string>> textListItems = new List<List<string>>();
public MainForm()
{
InitializeComponent();
}
private void cmdGo_Click(object sender, EventArgs e)
{
// store the contents of the list item text box
this.rawListItems = txtListItems.Text;
this.listItems.AddRange(Regex.Split(this.rawListItems, "\r\n"));
// setup min and max items - max items all items
int minItems = 3;
int maxItems = this.listItems.Count;
// We'll copy this list X times, X = same number of items in list
for (int i = 0; i < this.listItems.Count; i++)
{
// make a copy of the list items
List<string> listItemsCopy = new List<string>(this.listItems);
// get a random number between min items and max items
Random random = new Random();
int maxIndex = random.Next(minItems, maxItems + 1); // max is exclusive, hence the +1
// remove all elements after the maxIndex
for (int j = 0; j < listItemsCopy.Count; j++)
{
if (j > maxIndex)
{
listItemsCopy.RemoveAt(j);
}
}
// add the list copy to the master list
this.textListItems.Add(listItemsCopy);
// FIRST DEBUG MESSAGEBOX
String tst = "";
foreach (string item in listItemsCopy)
{
tst += item + " ## ";
}
MessageBox.Show(tst);
}
// SECOND DEBUG MESSAGEBOX
String output = "";
foreach (List<string> listitem in this.textListItems)
{
foreach (string item in listitem)
{
output += item + " ## ";
}
}
MessageBox.Show(output);
}
}
【问题讨论】:
标签: c#