定义一个实体类,对DataTime类型报错,不解

singer_101 2006-03-02 11:28:59
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类型啊,不解

...全文
239 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
singer_101 2006-03-02
  • 打赏
  • 举报
回复
对了,低级错误
因为数据库库里取出带时间,我想把他去掉,才这样,一楼和二楼是我之前做法。
解决方法:get{return DateTime.Parse(_birth.ToShortDateString());}
我再转一下就可以了

meixiaofeng 2006-03-02
  • 打赏
  • 举报
回复
get{return _birth;}
使用的时候再转换成string
LixingTie 2006-03-02
  • 打赏
  • 举报
回复
问题:
就是 get{return _birth.ToShortDateString();} 这句问题。
可我 _birth明明是DataTime类型啊,不解

======================================
_birth是DateTime没错,但"_birth.ToShortDateString()"是string类型,所以会报错。
ToShortDateString()方法是一个把DateTime类型转换成一个短日期类型字符串的方法。
namhyuk 2006-03-02
  • 打赏
  • 举报
回复
public DateTime BIRTH
{
set{ _birth=value;}
get{return _birth.ToShortDateString();}
}
有什么不解的?ToShortDateString()返回的就是string类型啊!
这个属性的返回值是DateTime,你应该get { return _birth; }
linuxyf 2006-03-02
  • 打赏
  • 举报
回复
DateTime是返回类型,为何要return _birth.ToShortDateString();
return _birth;试一下
真相重于对错 2006-03-02
  • 打赏
  • 举报
回复
get{return _birth;}

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧