コード例 #1
0
ファイル: MovieDB.cs プロジェクト: dec-k/IN710-keigdl1
        public void addDefaultFilms()
        {
            //Setup instances of movie to be pushed into the dictionary
            Movie movie1 = new Movie(1961, "Westside Story", "Jerome Robbins");
            Movie movie2 = new Movie(1972, "The Godfather", "Francis Ford Coppola");
            Movie movie3 = new Movie(1984, "Amadeus", "Milos Forman");
            Movie movie4 = new Movie(2007, "No Country for Old Men", "Ethan & Joel Coen");

            //Method calls to add the above movies to a dictionary.
            movieTable.Add(movie1.Year, movie1); //Passes in an explicit value of a movie (.Year) to use as a key.
            movieTable.Add(movie2.Year, movie2); //...and then pass in the entirety of the instance afterwards. (Duplicate data on .Year, though.)
            movieTable.Add(movie3.Year, movie3);
            movieTable.Add(movie4.Year, movie4);
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: dec-k/IN710-keigdl1
        //Not sure why this says button1, its clearly been renamed in the form.
        private void button1_Click(object sender, EventArgs e)
        {
            //If any required fields havent been filled, chuck an error message and do not add entry to dictionary.
            //For real simple checks like this it seems easier to just do an if statement. Multiple try-catches destroy my code elegance :(
            if (txtAddYear.Text == "" || txtAddDirector.Text == "" || txtAddTitle.Text == "")
            {
                MessageBox.Show("You have provided incorrect information for one or more fields.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                //Pull values from Year, Title, and Director textboxes.
                int key = Convert.ToInt16(txtAddYear.Text);
                string title = txtAddTitle.Text;
                string director = txtAddDirector.Text;

                //Concat these strings into something the movie dictionary can work with
                Movie newMovie = new Movie(key, title, director);

                //Make sure user doesn't enter a key that's already been used for another entry.
                //Not a critical error, no cleanup code required.
                try
                {
                    //Add it to the dictionary.
                    movDB.MovieTable.Add(key, newMovie);
                }
                catch(ArgumentException)
                {
                    MessageBox.Show("This year already has an entry. Please remove this years entry before adding another.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }