我有一个叫做电影的课。 Movie具有movieYear字符串属性和movieYearInt int属性。我想要一个类方法,我可以发送电影对象并获取一个已经获取movieYear字符串属性并将其值复制到movieYearInt的对象。稍后我会转换对象的更多属性。
这是电影类
public class movie
{
public string movieName { get; set; }
public string movieYear { get; set; }
public int movieYearInt { get; set; }
public movie convertFilm(movie m)
{
m.movieYearInt = int.Parse(m.movieYear);
return m;
}
}
这就是我想到以我的形式检索对象的想法
movie m = new movie();
m.movieYear = cmbMovieYear.ToString();
movie newMovie = m.convertFilm(m);
我得到的错误是:输入字符串的格式不正确 我做错了什么?
编辑 - 这是movieYear combobox
List<int> allYears = new List<int>();
allYears.AddRange(Enumerable.Range(1900, 113));
cmbMovieYear.DataSource = allYears;
编辑 -
m.movieYear = cmbMovieYear.SelectedItem.ToString();
这是解决方案。
答案 0 :(得分:0)
您是否考虑过将movieYearInt属性的getter修改为这样的内容,而不是返回已转换的对象?
public int movieYearInt
{
get
{
// add some validation, and consider using System.Int32.TryParse
return System.Int32.Parse(movieYear);
}
}
答案 1 :(得分:0)
public class movie: ICloneable
{
public string movieName { get; set; }
public string movieYear { get; set; }
public int movieYearInt { get { return int.Parse(movieYear) set {movieYear = value.ToString()}}
public object Clone()
{
return new movie() {movieName = this.movieName, movieYear = this.movieYear};
}
}
var m = new movie();
m.movieYear = "2001";
var newMovie = (movie) m.Clone();