0

我有一个由一个类填充的工作列表(或者我假设),并试图在表单上的一组文本框中显示唯一的记录。

public partial class frm_people : Form
{

    public frm_people()
    {
        // Loads the Form
        InitializeComponent();

        LoadData();

        ShowData();

    }

    // Global Variables

    private People peopleClass;
    private ArrayList peopleArrayList;

    private int numberOfPeople;
    private int currentPeopleShown;

    private void ShowData()
    {
        // Add to Text Box based on current Record
        txt_peopleName.Text = ((People)peopleArrayList[currentPeopleshown]).name;**
    }

    private void LoadData()
    {

        List<People> peopleList = new List<People>();

        People data = new People("James Bond", false, "Cardiff");

        peopleList.Add(data);

        numberOfPeople = 1;
        currentPeopleShown = 0;
    }
}

我收到一个错误(由 ** 指出):

“你调用的对象是空的。”

我知道类是通过引用使用的,如何尝试这种显示记录的方式?最终目标是能够通过使用currentPeopleShown变量在多个记录之间自由滚动。

4

4 回答 4

0

试试这个:

    private void ShowData()
    {
        // Add to Text Box based on current Record
      if(peopleArrayList[currentPeopleshown]!=null)
        txt_peopleName.Text = ((People)peopleArrayList[currentPeopleshown]).name;
    }
于 2013-04-09T16:35:05.510 回答
0

您的 peopleList 超出范围。

List<People> peopleList = new List<People>();

private void LoadData()
{
  //...
}

该数组没有被使用,所以使用 peopleList:

txt_peopleName.Text = peopleList[currentPeopleshown].name;

你不需要numberOfPeople变量,你可以使用peopleList.Count

于 2013-04-09T16:40:45.640 回答
0

你在哪里设置 peopleArrayList?

尝试以下几行:

private void LoadData()
{
    peopleArrayList = new ArrayList();
    People data = new People("James Bond", false, "Cardiff");

    peopleArrayList.Add(data);

    numberOfPeople = 1;
    currentPeopleShown = 0;
}
于 2013-04-09T16:41:09.053 回答
0

或者您可以一起消除 ArrayList 并执行此操作

public partial class frm_people : Form
{
   List<People> peopleList;
    public frm_people()
    {
        // Loads the Form
        InitializeComponent();

        peopleList = new List<People>();
        LoadData();

        ShowData();

    }

    // Global Variables

    private People peopleClass;

    private int numberOfPeople;
    private int currentPeopleShown;

    private void ShowData()
    {
        // Add to Text Box based on current Record
        txt_peopleName.Text = (peopleList[0]).name;**
    }

    private void LoadData()
    {

        People data = new People("James Bond", false, "Cardiff");

        peopleList.Add(data);

        numberOfPeople = 1;
        currentPeopleShown = 0;
    }
}
于 2013-04-09T16:42:21.643 回答