Ejemplo n.º 1
0
        private void ReadFromFile(string fileName)
        {
            try
            {
                //This allocates a new list to ShoalList, it resets already added contents to nothing by assigning it to a new list
                ShoalList = new List <Shoal>();

                using (StreamReader myInputStream
                           = new StreamReader(fileName))
                {
                    string eachLine, headerLine;
                    if (!myInputStream.EndOfStream)
                    {
                        //Header Line or first line of file typically has column names or header names (no numeric data),
                        //so it must be treated different
                        //than other lines in the file
                        headerLine = myInputStream.ReadLine();
                    }
                    while (!myInputStream.EndOfStream)
                    {
                        eachLine = myInputStream.ReadLine();
                        double[] lowTides    = new double[4];
                        double[] highTides   = new double[4];
                        string[] fieldsArray = eachLine.Split(',');
                        string   location    = fieldsArray[0];
                        string   state       = fieldsArray[1];
                        double.TryParse(fieldsArray[2],
                                        out double mileMarker);
                        string   lowTidesStr  = fieldsArray[3];
                        string   highTidesStr = fieldsArray[4];
                        string[] lowTidesStrArray
                            = lowTidesStr.Split('-');
                        for (int i = 0; i < lowTidesStrArray.Length; i++)
                        {
                            double.TryParse(lowTidesStrArray[i],
                                            out lowTides[i]);
                        }
                        string[] highTidesStrArray
                            = highTidesStr.Split('-');
                        for (int i = 0; i < highTidesStrArray.Length; i++)
                        {
                            double.TryParse(highTidesStrArray[i],
                                            out highTides[i]);
                        }
                        Shoal eachShoal = new Shoal(location, state,
                                                    mileMarker, lowTides, highTides);

                        ShoalList.Add(eachShoal);
                    }
                }
            } catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Ejemplo n.º 2
0
        private void AddItem()
        {
            //This method gets all values from TextBoxes, converts numeric values into numbers from string
            //got from Text property, create a shoal object, and then add it to the list

            //you can add several else if for validation if you like

            if (locationTextBox.Text == "")
            {
                MessageBox.Show("Location cannot be empty");
            }
            else
            {
                //Two New 4 element arrays to maintain low tide and high tide depths
                double[] lowTides  = new double[4];
                double[] highTides = new double[4];

                //mileMarker is int so needs to be parsed from Text property of mile marker text box
                double mileMarker;

                try
                {
                    //just using Parse to show use with try catch. Better to use TryParse in c# though.

                    lowTides[0]  = double.Parse(lowTidesTB1.Text);
                    lowTides[1]  = double.Parse(lowTidesTB2.Text);
                    lowTides[2]  = double.Parse(lowTidesTB3.Text);
                    lowTides[3]  = double.Parse(lowTidesTB4.Text);
                    highTides[0] = double.Parse(highTidesTB1.Text);
                    highTides[1] = double.Parse(highTidesTB2.Text);
                    highTides[2] = double.Parse(highTidesTB3.Text);
                    highTides[3] = double.Parse(highTidesTB4.Text);
                    //  mileMarker = double.Parse(mileMarkerTextBox.Text);
                    double.TryParse(mileMarkerTextBox.Text, out mileMarker); //example using TryParse
                    string location = locationTextBox.Text;
                    string state    = stateTextBox.Text;
                    //creating a shoal object with all this information, check Shoal Class definition, and constructor definition to see
                    //how the constructor is used o create shoal object

                    Shoal eachShoal = new Shoal(location, state, mileMarker,
                                                lowTides, highTides);
                    // MessageBox.Show(eachShoal.ToString());

                    //This shoal list maintains a list of shoal objects added so far.
                    ShoalList.Add(eachShoal); //no new list, list is created when Form1 is created
                                              //ShoalList.Add("Any shoal"); //ShoalList can only have a shoal object added to it
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
Ejemplo n.º 3
0
        private void UpdateItem()
        {
            int listIndex = outputListBox.SelectedIndex - 1;

            if (listIndex < 0)
            {
                MessageBox.Show("Please select item from output listbox to update");
            }
            else
            {
                double[] lowTides  = new double[4];
                double[] highTides = new double[4];
                double   mileMarker;
                try
                {
                    double.TryParse(lowTidesTB1.Text, out lowTides[0]);
                    double.TryParse(lowTidesTB2.Text, out lowTides[1]);
                    double.TryParse(lowTidesTB3.Text, out lowTides[2]);
                    double.TryParse(lowTidesTB4.Text, out lowTides[3]);
                    double.TryParse(highTidesTB1.Text, out highTides[0]);
                    double.TryParse(highTidesTB2.Text, out highTides[1]);
                    double.TryParse(highTidesTB3.Text, out highTides[2]);
                    double.TryParse(highTidesTB4.Text, out highTides[3]);
                    mileMarker = double.Parse(mileMarkerTextBox.Text);
                    string location  = locationTextBox.Text;
                    string state     = stateTextBox.Text;
                    Shoal  eachShoal = new Shoal(location, state, mileMarker,
                                                 lowTides, highTides);
                    // MessageBox.Show(eachShoal.ToString());
                    ShoalList[listIndex] = eachShoal;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }