将字符串转换为int,int转换为字符串
我正在使用添加了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);
}
看起来你只是想解析值作为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;
}
谢谢!那确实欺骗了! –
@KevinWare,看看帮助 - >游览网站礼仪。如果找到解决方案,请检查它,以便其他用途知道它不再是未清项目。 – DRapp
看起来像你想Int32.TryParse:
if (Int32.TryParse(txtBox_Length.Text, out newMovie.movieLength))
{
txtBox_Length.Background = new SolidColorBrush(Colors.LightGray);
}
我知道,它不应该是isItDate或ConvertStringToDate,但我无法弄清楚使用什么语法,所以我只是为了一致性而离开它。 –
这是我发布的方法,我试图转换为int,但它不成功。你到底在问什么? –
'ConvertStringToDate()'的定义/方法签名 –