如何把字符串转换为int?类型?

onekey 2008-03-12 03:32:06
int? Position_ID;

Position_ID = string.IsNullOrEmpty(pid) ? null : int.Parse(pid);

这样编译时候出错:Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'int'

如何解决?
...全文
326 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovefootball 2008-03-12
  • 打赏
  • 举报
回复
哦,Sorry,TryParse有问题
还是像楼上那样转一下吧~~
onekey 2008-03-12
  • 打赏
  • 举报
回复
7楼正解
lovefootball 2008-03-12
  • 打赏
  • 举报
回复
这是三元操作符的问题
可以参考下
http://msdn2.microsoft.com/en-us/library/aa691313(VS.71).aspx

你可以把代码写成如下

int? Position_ID = null;
if (!String.IsNullOrEmpty(pid))
{
int.TryParse(pid, out Position_ID);
}
vwxyzh 2008-03-12
  • 打赏
  • 举报
回复
Position_ID = string.IsNullOrEmpty(pid) ? null : (int?)int.Parse(pid);
jiao545214147 2008-03-12
  • 打赏
  • 举报
回复
string str ="0";
int i;
i=int.parse(str);
onekey 2008-03-12
  • 打赏
  • 举报
回复
Position_ID是要插入数据库的,
不能赋0
h_w_king 2008-03-12
  • 打赏
  • 举报
回复
Nullable<int> Position_ID;
SaintKaKa 2008-03-12
  • 打赏
  • 举报
回复
string为空时不能转int的
可以用2楼的方法赋0值给Position_ID
yuanmanguo 2008-03-12
  • 打赏
  • 举报
回复
Position_ID = string.IsNullOrEmpty(pid) ? 0 : int.Parse(pid);
山之魂2 2008-03-12
  • 打赏
  • 举报
回复
int 不能赋空值

110,538

社区成员

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

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

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