asp.net中的datetime属性和字段 [英] datetime properties and fields in asp.net

查看:78
本文介绍了asp.net中的datetime属性和字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DateTime _hiredate;
        public DateTime? hiredate
        {
            get
            {
                return _hiredate;
            }
            set
            {
                _hiredate = value;
            }
        }


<br />
Error    1    Cannot implicitly convert type ''System.DateTime?'' to ''System.DateTime''. An explicit conversion exists (are you missing a cast?)    E:\aspnet\comp\Business Object\company.cs    64    29    Business Object



我遇到转换问题,请帮助我

thanq.



i am getting conversion problem please help me

thanq.

推荐答案

,您需要将_hiredate声明为可为空的日期时间,或者在getter中进行强制转换,并在setter中使用HasValue Value

我建议您将_hiredate字段用作可为空的DateTime,例如
you need to either declare _hiredate as a nullable datetime or cast in the getter and use HasValue Value in the setter

I suggest you just the _hiredate field as a nullable DateTime, e.g.
DateTime? _hiredate;


因为您指定了属性为可空值:
Because you have specified that your property is nullable:
public DateTime? hiredate

编译器期望可为null的类型-DateTime不是.在设置值之前先进行投射:

the compiler expects a nullable type - which DateTime isn''t. Cast it before you set the value:

public DateTime? hiredate
{
    get
    {
        return _hiredate;
    }
    set
    {
        _hiredate = (DateTime) value;
    }
}

或更妙的是,使您的媒体资源字段可为空.甚至更好,请使用自动属性.

Or better, make your property base field nullable. Or even better, use an automatic property.

public DateTime? hiredate { get; set; }


这篇关于asp.net中的datetime属性和字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆