我在向文本框发送List<>
时遇到问题,我不确定是什么问题。我已经检查过,因此List实际上有值,并且它可以从类中正确地转移到这个块中。
代码:
public void Listtest(List<string> m_customers)
{
lstRegistry.Items.Clear();
for (int index = 0; index == m_customers.Count; index++)
{
lstRegistry.Items.Add(m_customers[index]);
}
}
发送List<>
class CustomManager
{
//private List<Customer> m_customers;
List<string> m_customers = new List<string>();
public void CreateNewString(string adresslist, string emaillist, string phonelist, string namelist)
{
MainForm strIn = new MainForm();
string newlist = string.Format("{0,-3} {1, -10} {2, -20} {3, -30}", namelist, phonelist, emaillist, adresslist);
m_customers.Add(newlist); //líst is created.
strIn.Listtest(m_customers);
}
}
我不能让它工作,我真的被卡住了。 :/
感谢您提供任何帮助和想法!!!
//此致
答案 0 :(得分:9)
将循环条件更改为:index < m_customers.Count
修改强> 您还可以为此数据创建一个类:
class Person
{
public string Name {get; set;}
public string Address {get; set;}
public string Email {get; set;}
}
因此,您可以列出人员列表:List<Person>
答案 1 :(得分:2)
Erno的回答应该解决您的问题,但我也建议您阅读foreach。使用它会改变您的代码:
for (int index = 0; index < m_customers.Count; index++)
{
lstRegistry.Items.Add(m_customers[index]);
}
到
foreach (string cust in m_customers)
{
lstRegistry.Items.Add(cust );
}
我认为这更容易阅读。