简体   繁体   English

条件运算符。 错误CS0266无法将类型'int'隐式转换为'byte'

[英]Conditional operator. Error CS0266 Cannot implicitly convert type 'int' to 'byte'

I need some help on this conversion. 我需要有关此转换的帮助。 I have decompiled an old dll file, because I need some small changes and I do not have the original sourcecode, but before I start making these changes I would like the original code to compile. 我已经反编译了一个旧的dll文件,因为我需要进行一些小的更改并且我没有原始的源代码,但是在开始进行这些更改之前,我希望可以编译原始的代码。

I only got one issue: 我只有一个问题:

Error CS0266 Cannot implicitly convert type 'int' to 'byte'. 错误CS0266无法将类型'int'隐式转换为'byte'。 An explicit conversion exists (are you missing a cast?) 存在显式转换(您是否缺少演员表?)

I have tried many different solutions but all of the fails, so please help. 我尝试了许多不同的解决方案,但是所有失败的方法,因此请提供帮助。 How to fix this conversion? 如何解决此转换?

public static byte SqlGetByte(DbDataReader sr, int col, ConnectionString connection)
{
    return (sr[col] == DBNull.Value) ? 0 : (connection.OraDb ? Convert.ToByte(sr.GetDecimal(col)) : sr.GetByte(col));
}

Just cast to byte 只需转换为byte

return (byte)((sr[col] == DBNull.Value) ? 0 : (connection.OraDb ? Convert.ToByte(sr.GetDecimal(col)) : sr.GetByte(col)));

// or

return (sr[col] == DBNull.Value) ? (byte)0 : (connection.OraDb ? Convert.ToByte(sr.GetDecimal(col)) : sr.GetByte(col));

From the specs 从规格

12.15 Conditional operator 12.15条件运算符

... A conditional expression of the form b ? ...形式为b的条件表达式? x : y first evaluates the condition b ... x:y首先评估条件b ...

The second and third operands, x and y, of the ?: operator control the type of the conditional expression. ?:运算符的第二和第三操作数x和y控制条件表达式的类型。

  • If x has type X and y has type Y then, 如果x的类型为X,y的类型为Y,

    • If X and Y are the same type, then this is the type of the conditional expression. 如果X和Y是同一类型,则这是条件表达式的类型。
    • Otherwise, if an implicit conversion (§11.2) exists from X to Y, but not from Y to X, then Y is the type of the conditional expression. 否则,如果存在从X到Y的隐式转换(第11.2节),但从Y到X不存在,则Y是条件表达式的类型。
    • Otherwise, if an implicit enumeration conversion (§11.2.4) exists from X to Y, then Y is the type of the conditional expression. 否则,如果存在从X到Y的隐式枚举转换(第11.2.4节),则Y是条件表达式的类型。
    • Otherwise, if an implicit enumeration conversion (§11.2.4) exists from Y to X, then X is the type of the conditional expression. 否则,如果存在从Y到X的隐式枚举转换(第11.2.4节),则X是条件表达式的类型。
    • Otherwise, if an implicit conversion (§11.2) exists from Y to X, but not from X to Y , then X is the type of the conditional expression . 否则,如果存在从Y到X的隐式转换(第11.2节) ,但是从X到Y不存在 ,则X是条件表达式的类型
    • Otherwise, no expression type can be determined, and a compile-time error occurs. 否则,将无法确定表达式类型,并且会发生编译时错误。
  • If only one of x and y has a type, and both x and y are implicitly convertible to that type, then that is the type of the conditional expression. 如果x和y中只有一个具有类型,并且x和y都可以隐式转换为该类型,则这就是条件表达式的类型。
  • Otherwise, no expression type can be determined, and a compile-time error occurs. 否则,将无法确定表达式类型,并且会发生编译时错误。

Given 给定

return condition ? x(int) : y(condition ? x2(byte)) : y2(byte));

Example

byte y = 123;
int x = 234;

x = y; // y can implicitly convert to x
y = x; // error, x cant implicitly convert to y

In summary, the resulting type of the Conditional Operator wants to be an int and you are returning a byte from your method. 总之, 条件运算符的结果类型希望是一个int并且您要从方法中返回一个byte Computer says no... At the very least you will need to cast somewhere 计算机说不...至少您需要在某个地方投射

If sr[col] == DBNull.Value equates to true, you are returning 0 whereas the method returns a byte . 如果sr[col] == DBNull.Value等于true,则返回0,而该方法返回一个byte

Change it to: 更改为:

return (sr[col] == DBNull.Value) ? Convert.ToByte(0) : (connection.OraDb ? Convert.ToByte(sr.GetDecimal(col)) : sr.GetByte(col));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误CS0266:无法将类型&#39;object&#39;隐式转换为&#39;int&#39; - error CS0266: Cannot implicitly convert type 'object' to 'int' 错误:CS0266无法将类型&#39;int&#39;隐式转换为&#39;byte&#39;。 存在显式转换(您是否缺少演员表?) - Error: CS0266 Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?) 错误 CS0266:无法将类型“float”隐式转换为“int”。 存在显式转换(您是否缺少演员表?) - error CS0266: Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?) 清单<t> - 编译器错误 CS0266 - 无法隐式转换类型</t> - IList<T> - Compiler Error CS0266 - Cannot implicitly convert type C#错误CS0266:无法将类型“狗”隐式转换为“ Rottweiler” - C# error CS0266: Cannot implicitly convert type 'Dog' to 'Rottweiler CS0266 无法转换类型 - CS0266 cannot convert type 错误 CS0266 无法将类型“Newtonsoft.Json.Linq.JObject”隐式转换为“字符串”。 存在显式转换(您是否缺少演员表?) - Error CS0266 Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' to 'string'. An explicit conversion exists (are you missing a cast?) CS0266-无法将类型“ System IList”隐式转换为“ System ArrayList”-软件测试 - CS0266 - Cannot implicitly convert type “System IList” to “System ArrayList” - Software Testing CS0266 无法隐式转换类型 'System.Collections.Generic.IEnumerable<char> '到'字符串'</char> - CS0266 cannot implicitly convert type 'System.Collections.Generic.IEnumerable<char>' to 'string' C# CosmosDb 查询错误 CS0266:无法隐式转换类型 'System.Linq.IQueryable <system.collections.generic.list<xxxx> ' 到</system.collections.generic.list<xxxx> - C# CosmosDb Query error CS0266: Cannot implicitly convert type 'System.Linq.IQueryable<System.Collections.Generic.List<xxxx>' to
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM