1

I'm working with a WPF file with 2 classes added. I have a Movie class, a Movie Utility class, the MainWindow.xaml, and the MainWindow.xaml.cs. My program is taking questions about a movie, entered in on the .xaml window, and produces a data grid underneath. In the Movie class, I have all of my public strings and dateline's. Like so:

namespace FinalExam
{
    public class Movie
    {
        public string movieName { get; set; }

        public DateTime releaseDate { get; set; }

        public string onDVD { get; set; }

        public string onBluRay { get; set; }

        public string genreType { get; set; }

        public string directorName { get; set; }

        public string producerName { get; set; }

        public int movieLength { get; set; }

        public string moveRating { get; set; }        
    }
}

In the MainWindow.xaml.cs class, all of the strings are called, and the dateline's are converted to string using the ConvertStringToDate syntax.

The problem I'm running into is getting the MovieLength to work. I have it entered as an into but don't know how to go about getting it to show up in the grid without it tripping my boo. Anyway, here's what I have.

namespace FinalExam
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //GLOBAL VARIABLE AREA
        List<Movie> movieList = new List<Movie>();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void but_Submit_Click(object sender, RoutedEventArgs e)
        {
            bool isGoodToAddNewMovie = true;
            Movie newMovie = new Movie();
            newMovie.movieName = txtBox_MovieName.Text;
            newMovie.onDVD = txtBox_DVD.Text;
            newMovie.onBluRay = txtBox_BluRay.Text;
            newMovie.genreType = txtBox_Genre.Text;
            newMovie.directorName = txtBox_Director.Text;
            newMovie.producerName = txtBox_Producer.Text;
            newMovie.moveRating = txtBox_Rating.Text;

            if (MovieUtility.isItDate(txtBox_ReleaseDate.Text))
            {
                newMovie.releaseDate = MovieUtility.ConvertStringToDate(txtBox_ReleaseDate.Text);
                txtBox_ReleaseDate.Background = new SolidColorBrush(Colors.LightGray);
            }
            else
            {
                txtBox_ReleaseDate.Background = new SolidColorBrush(Colors.Red);
                isGoodToAddNewMovie = false;
            }

            if (MovieUtility.isItDate(txtBox_Length.Text))
            {
                newMovie.movieLength = MovieUtility.ConvertStringToDate(txtBox_Length.Text);
                txtBox_Length.Background = new SolidColorBrush(Colors.LightGray);
            }
            else
            {
                txtBox_Length.Background = new SolidColorBrush(Colors.Red);
                MessageBox.Show("Please enter movie length in minutes.");
                isGoodToAddNewMovie = false;
            }

            //ADD PERSON TO LIST
            if (isGoodToAddNewMovie)
            {
                movieList.Add(newMovie);
                dataGrid_Movies.ItemsSource = new List<Movie>(movieList);
            }
        }
    }
}

And this is the issue:

if (MovieUtility.isItDate(txtBox_Length.Text))
{
    newMovie.movieLength = MovieUtility.ConvertStringToDate(txtBox_Length.Text);
    txtBox_Length.Background = new SolidColorBrush(Colors.LightGray);
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88

2 Answers2

1

It looks like you just want to parse the value as an int, not DateTime. You can test the value first, by using int.TryParse, which returns true if the value is a valid integer:

int length;

if (int.TryParse(txtBox_Length.Text, out length))
{
    newMovie.movieLength = length;
    txtBox_Length.Background = new SolidColorBrush(Colors.LightGray);
}
else
{
    txtBox_Length.Background = new SolidColorBrush(Colors.Red);
    MessageBox.Show("Please enter movie length in minutes.");
    isGoodToAddNewMovie = false;
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
0

Looks like you want Int32.TryParse:

if (Int32.TryParse(txtBox_Length.Text, out newMovie.movieLength))
{
    txtBox_Length.Background = new SolidColorBrush(Colors.LightGray);
}
Ian
  • 1,475
  • 2
  • 15
  • 31