1

我无法访问列表中的第二项,我不明白为什么会有任何帮助。

class Datapoints
    {
        List<Datapoint> datapoints;                                         //Creates a list of datapoints from Datapoint
        private string fileName;
        public string Filename { get { return fileName; } }

        public Datapoint GetDatapoint(int i)                                //Creates an instance of a Datapoint called GetDatapoint                        
        {
            if (i < datapoints.Count)
                return datapoints[i];
            else
                return null;
        }

        public Datapoints(string fName)                                     //Method that creates a new list of datapoints with the objects of Datapoint within it
        {
            this.fileName = fName;
            datapoints = new List<Datapoint>();
            TextReader tr = new StreamReader(fileName);
            string input;
            while ((input = tr.ReadLine()) != null)
            {
                string[] bits = input.Split(',');
                Datapoint a = new Datapoint(bits[0], bits[1], bits[2]);
                datapoints.Add(a);
            }
            tr.Close();

        }

这是表格,我想要做的是让框在按下下一个按钮时显示文件中的三个数字。

private void InitTextBoxes()
        {
            if (myDatapoints.Count > 0)
            {
                Datapoint a = myDatapoints.getItem(0);
                textBoxLatitude.Text = a.Latitude;
                textBoxLongtitude.Text = a.Longtitude;
                textBoxElevation.Text = a.Elevation;
                buttonNext.Enabled = true;
                buttonPrevious.Enabled = true;
            }
            count = 0;
            textBoxLatitude.Enabled = false;
            textBoxLongtitude.Enabled = false;
            textBoxElevation.Enabled = false;
            buttonDone.Visible = false;
            buttonDone.Enabled = false;
            addingData = false;
            saved = true;
            openToolStripMenuItem.Enabled = false;
        }


        private void openToolStripMenuItem1_Click(object sender, EventArgs e)
        {

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Csv Files (*.csv)|*.csv|Text files (*.txt)|*.txt|All files (*.*)|*.*";
            if (ofd.ShowDialog(this).Equals(DialogResult.OK))
            {
                myDatapoints = new Datapoints(ofd.FileName);
                this.Text = "Data Entry - " + ofd.SafeFileName;
                InitTextBoxes();
                if (myDatapoints.Count > 0)
                {
                    buttonNext.Enabled = true;
                    buttonPrevious.Enabled = true;
                    closeToolStripMenuItem.Enabled = true;
                    saveAsToolStripMenuItem.Enabled = true;
                }
            }

        }



        private void buttonNext_Click(object sender, EventArgs e)
        {


                count++;
                if (count == myDatapoints.Count)
                    count = 0;

            Datapoint a = myDatapoints.getItem(count);
            textBoxLatitude.Text = a.Latitude;
            textBoxLongtitude.Text = a.Longtitude;
            textBoxElevation.Text = a.Elevation;

            textBoxTest.Text = Convert.ToString(myDatapoints.Count);

        }

        private void buttonPrevious_Click(object sender, EventArgs e)
        {

        } 

此外,当我启动这些框时,我无法将 datapoints.GetItem 更改为除 (0) 之外的任何内容,就好像我这样做了,下一行中有错误我相信这是问题的核心,但我不明白为什么。错误是

你调用的对象是空的。

在我用来测试的文件中,有 9 个数字(1、2、3、4、5、6、7、8、9),所以当我选择 1 时,它应该取中间三个,但它没有!

 internal Datapoint getItem(int p)
        {
            if (p < datapoints.Count)
            {
                return datapoints[p];
            }
            else
                return null;
        }
4

1 回答 1

0

在 datapoints 构造函数中,您只存储前三个(如果您的文件仅包含一行)

要么将文件格式更改为每行只有 3 个数字,例如:
1,2,3
4,5,6
7,8,9

或更改逻辑以存储单行中的所有值,例如:

   while ((input = tr.ReadLine()) != null)
        {
            string[] bits = input.Split(',');
            if (bits.Length % 3 != 0)
            {
               //error bad file 
            }
            else
            {
               int count = bits.Length / 3;
               int i = 0;
               while (count > 0)
               {
                   Datapoint a = new Datapoint(bits[i], bits[i+1], bits[i+2]);
                   datapoints.Add(a);
                   i += 3;
                   count--;
               }
             }
        }
于 2012-12-13T19:27:28.327 回答