using System;namespace Entity
{ public class TUSERINFO
{
public TUSERINFO(){}
#region Entity实体
private int _userid;
private string _username;
private DateTime _birth;
/// <summary>
///字段名称:人员ID
/// <summary>
public int USERID
{
set{ _userid=value;}
get{return _userid;}
}
/// <summary>
///字段名称:姓名
/// <summary>
public string USERNAME
{
set{ _username=value;}
get{return _username;}
}
/// <summary>
///字段名称:出生年月
/// <summary>
public DateTime BIRTH
{
set{ _birth=value;}
get{return _birth.ToShortDateString();}
}
#endregion Entity实体
}
}报错:TUSERINFO.cs: 无法将类型“string”隐式转换为“System.DateTime”
就是 get{return _birth.ToShortDateString();} 这句问题。
可我 _birth明明是DataTime类型啊,不解
{ public class TUSERINFO
{
public TUSERINFO(){}
#region Entity实体
private int _userid;
private string _username;
private DateTime _birth;
/// <summary>
///字段名称:人员ID
/// <summary>
public int USERID
{
set{ _userid=value;}
get{return _userid;}
}
/// <summary>
///字段名称:姓名
/// <summary>
public string USERNAME
{
set{ _username=value;}
get{return _username;}
}
/// <summary>
///字段名称:出生年月
/// <summary>
public DateTime BIRTH
{
set{ _birth=value;}
get{return _birth.ToShortDateString();}
}
#endregion Entity实体
}
}报错:TUSERINFO.cs: 无法将类型“string”隐式转换为“System.DateTime”
就是 get{return _birth.ToShortDateString();} 这句问题。
可我 _birth明明是DataTime类型啊,不解
return _birth;试一下
{
set{ _birth=value;}
get{return _birth.ToShortDateString();}
}
有什么不解的?ToShortDateString()返回的就是string类型啊!
这个属性的返回值是DateTime,你应该get { return _birth; }
就是 get{return _birth.ToShortDateString();} 这句问题。
可我 _birth明明是DataTime类型啊,不解======================================
_birth是DateTime没错,但"_birth.ToShortDateString()"是string类型,所以会报错。
ToShortDateString()方法是一个把DateTime类型转换成一个短日期类型字符串的方法。
使用的时候再转换成string
因为数据库库里取出带时间,我想把他去掉,才这样,一楼和二楼是我之前做法。
解决方法:get{return DateTime.Parse(_birth.ToShortDateString());}
我再转一下就可以了