[英]No implicit conversion between int and string
When i try to execute this belo code i'm getting that error. 当我尝试执行此belo代码时,出现该错误。
//Code: //码:
int Value = Convert.ToInt32(Request.QueryString["Value"] == null ? 0 : Request.QueryString["Value"]);
So i need to pass the value '0' if the QueryString
value is null. 因此,如果
QueryString
值为null,则需要传递值'0'。
How can i solve this? 我该如何解决?
int Value = Convert.ToInt32(Request.QueryString["Value"] ?? "0");
You could pass the string "0"
but a better way would be: 您可以传递字符串
"0"
但是更好的方法是:
int Value = Request.QueryString["Value"] == null ? 0 : Convert.ToInt32(Request.QueryString["Value"]);
and you could also factor out the lookup: 您还可以考虑查找:
string str = Request.QueryString["Value"];
int value = str == null ? 0 : Convert.ToInt32(str);
Try this 尝试这个
int Value = Convert.ToInt32(Request.QueryString["Value"] == null ? "0" : Request.QueryString["Value"]);
Or take the advantage of ??
还是利用
??
operator 算子
int Value = Convert.ToInt32(Request.QueryString["Value"] ?? "0");
Your false and true statement in Ternary operator should be of same type or should be implicitly convertible to another. 三元运算符中的错误和真实语句应为相同类型或应隐式转换为另一个。
Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.
first_expression和second_expression的类型必须相同,或者必须存在从一种类型到另一种类型的隐式转换。
Try this: 尝试这个:
int i;
int.TryParse(Request.QueryString["Value"], out i);
if parsing will fail i
will have default value (0) without explicit assignment and checking if query string is null. 如果解析将失败,
i
将具有默认值(0),而无需显式分配并检查查询字符串是否为null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.