简体   繁体   English

int和字符串之间没有隐式转换

[英]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的类型必须相同,或者必须存在从一种类型到另一种类型的隐式转换。

Taken from msdn 取自msdn

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.

相关问题 C# - “int”和“int”之间没有隐式转换<null> - C# - There is no implicit conversion between 'int' and <null> 无法确定条件表达式的类型,因为在&#39;string&#39;和&#39;int&#39;之间没有隐式转换 - Type of conditional expression cannot be determined because there is no implicit conversion between 'string' and 'int' 无法确定条件表达式的类型,因为“int”和“string”之间没有隐式转换 - Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and 'string' 无法确定条件表达式的类型,因为&#39;int?&#39;之间没有隐式转换。 和“字符串” - Type of conditional expression cannot be determined because there is no implicit conversion between 'int?' and 'string' 字符串和系统窗口表单对话框之间没有隐式转换 - no implicit conversion between string and system windows form dialog 尝试将null分配给可为null的int会导致“在&#39;int&#39;和&#39;null&#39;之间没有隐式转换”错误 - Attempting to assign null to nullable int results in “There is no implicit conversion between 'int' and 'null'” error 从int列获取值会导致“在&#39;int&#39;和null之间没有隐式转换”错误 - Getting value from an int column causes “no implicit conversion between 'int' and null” error “ void”和“之间的隐式转换 <null> &#39;? - implicit conversion between 'void' and '<null>'? 等效接口之间的隐式转换 - Implicit conversion between equivalent interfaces null和null之间没有隐式转换 - There is no implicit conversion between null and null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM