I am having trouble accessing the second item in the list and I cannot see why any help would be appreciated.
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();
}
Here is the form, what I am trying to make happen is to have the boxes show the three numbers in the file when pressing the next button.
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)
{
}
Also, when I initiate the boxes, I cannot change the datapoints.GetItem to anything but (0) as if i do there is an error in the following line I believe this is the heart of the problem but I cannot see why. The error is
Object reference not set to an instance of an object.
Within the file I am using to test there are 9 numbers (1,2,3,4,5,6,7,8,9) so it ought to take the middle three when I select 1, bit it doesn’t!
internal Datapoint getItem(int p)
{
if (p < datapoints.Count)
{
return datapoints[p];
}
else
return null;
}
in the datapoints constructor you are only storing the first three (if you file contains only one line)
either change file format to only have 3 numbers per line
ex:
1,2,3
4,5,6
7,8,9
or change logic to store all values from single line ex: