2015-12-14 378 views
1

我正在使用添加了2個類的WPF文件。我有一個Movie類,一個Movie Utility類,MainWindow.xaml和MainWindow.xaml.cs。我的程序正在回答有關電影的問題,並在.xaml窗口中輸入,並在下方生成一個數據網格。在電影類中,我擁有我所有的公共字符串和日期線。像這樣:將字符串轉換爲int,int轉換爲字符串

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; }   
    } 
} 

在MainWindow.xaml.cs類,所有的琴絃被稱爲和國際日期變更線的使用ConvertStringToDate語法轉換成字符串。

我遇到的問題是讓MovieLength工作。我把它作爲一個進入,但不知道如何去讓它出現在網格中,而不會跳過我的噓聲。無論如何,這是我的。

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); 
      } 
     } 
    } 
} 

這是問題:

if (MovieUtility.isItDate(txtBox_Length.Text)) 
{ 
    newMovie.movieLength = MovieUtility.ConvertStringToDate(txtBox_Length.Text); 
    txtBox_Length.Background = new SolidColorBrush(Colors.LightGray); 
} 
+0

我知道,它不應該是isItDate或ConvertStringToDate,但我無法弄清楚使用什麼語法,所以我只是爲了一致性而離開它。 –

+0

這是我發佈的方法,我試圖轉換爲int,但它不成功。你到底在問什麼? –

+0

'ConvertStringToDate()'的定義/方法簽名 –

回答

1

看起來你只是想解析值作爲int,不DateTime。您可以先測試值,通過使用int.TryParse,返回true如果值是一個有效的整數:

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; 
} 
+0

謝謝!那確實欺騙了! –

+2

@KevinWare,看看幫助 - >遊覽網站禮儀。如果找到解決方案,請檢查它,以便其他用途知道它不再是未清項目。 – DRapp

0

看起來像你想Int32.TryParse

if (Int32.TryParse(txtBox_Length.Text, out newMovie.movieLength)) 
{ 
    txtBox_Length.Background = new SolidColorBrush(Colors.LightGray); 
}