Type of conditional expression cannot be determined because there is no implicit conversion between ...

本文介绍了在.NET中处理枚举类型时遇到的问题,特别是当需要对枚举类型的NULL值进行赋值操作时的解决方案。通过使用三元运算符并确保NULL被正确转换为枚举类型的具体值,避免了类型转换错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public EnumType.EnumGender? EmployeeGender
        {
            get { return (EnumType.EnumGender)_Model.EmployeeGender; }
            set { _Model.EmployeeGender = value != null ? (int)value : (int)EnumType.EnumGender.无限制;}
        }

今天用三元运算符给EmployeeGender赋值时,出现Type of conditional expression cannot be determined because there is no implicit conversion between 'char' and '<null>'错误

经查资料才明白,给EnumType.EnumGender赋值时必须对NULL进行转换,才可以.

代码如下:

bll.EmployeeGender=Request.Form["EmployeeGender"]!=""?(Common.EnumType.EnumGender)(int.Parse(Request.Form["EmployeeGender"])):(Common.EnumType.EnumGender?)null;

参考资料:http://social.msdn.microsoft.com/Forums/zh-TW/csharpgeneral/thread/506a8166-3113-4965-be51-d2a41f0fc01e

转载于:https://www.cnblogs.com/cicada/archive/2013/01/16/2862789.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值