public UserAccount(List<string> data)
{
this.UserIDNumber = data[0]; //issue here
this.name = data[1];
this.Username = data[2];
this.Password = data[3];
this.Birthday = data[4]; //issue here
this.Address = data[5];
this.City = data[6];
}
我遇到的问题是我无法转换两种数据类型。如果我使用this.Birthday.Tostring()
和this.UserIDNumber.ToString()
我收到错误:作业的左侧必须是var,prop或indexer。
答案 0 :(得分:4)
您需要将字符串显式转换为正确的类型。
public UserAccount(List<string> data)
{
this.UserIDNumber = Convert.ToInt32(data[0]);
this.name = data[1];
this.Username = data[2];
this.Password = data[3];
this.Birthday = DateTime.Parse(data[4]);
this.Address = data[5];
this.City = data[6];
}
或者安全的方式,如果转换失败,则不会抛出异常。但请记住,如果您没有处理默认值,那么您可以获得损坏的数据。
public UserAccount(List<string> data)
{
int id;
this.UserIDNumber = int.TryParse(data[0], out id) ? id : 0;
this.name = data[1];
this.Username = data[2];
this.Password = data[3];
DateTime date;
this.Birthday = DateTime.TryParse(data[4], out date) ? date : DateTime.MinValue;
this.Address = data[5];
this.City = data[6];
}
答案 1 :(得分:1)
试试这个:
this.UserIDNumber = Convert.ToInt32(data[0]);
this.Birthday = DateTime.Parse(data[4]);
答案 2 :(得分:0)
我最终做了什么(对于这个和日期时间)
this.ErrorMessage = string.Empty;
int parseInt = 0;
if (!int.TryParse(data[0], out parseInt))
{
this.ErrorMessage += "Error Parsing the account number! data was: " + data[0];
this.AcctNumber = -1;
}
else
{
this.AcctNumber = parseInt;
}