2011-04-02 16 views
17

Cuando compilo mi proyecto de C# en MonoDevelop, me sale el siguiente error:Tipo de expresión condicional no se puede determinar como

Type of conditional expression cannot be determined as 'byte' and 'int' convert implicitly to each other

Fragmento de código:

byte oldType = type; 
type = bindings[type]; 
//Ignores updating blocks that are the same and send block only to the player 
if (b == (byte)((painting || action == 1) ? type : 0)) 
{ 
    if (painting || oldType != type) { SendBlockchange(x, y, z, b); } return; 
} 

Esta es la línea que se destaca en el error:

if (b == (byte)((painting || action == 1) ? type : 0))

¡La ayuda es muy apreciada!

Respuesta

23

El operador condicional es una expresión y, por lo tanto, necesita un tipo de devolución, y ambas rutas deben tener el mismo tipo de devolución.

(painting || action == 1) ? type : (byte)0 
+0

que tenga sentido. ¡Muchas gracias! – Jakir00

+1

¿Qué tal si el parámetro se espera que sea de cualquier tipo, como 'String.Format (" value: {0} ", (value == null)?:" Null ": value)' donde el valor es de tipo 'int?'? – mr5

4

no hay conversión implícita entre byte y int, por lo que necesita para especificar uno de los resultados del operador ternario:

? type : (byte)0 

Ambos tipos de retorno de este operador tienen que ser o bien el mismo o tiene una conversión implícita definida para poder funcionar.

De MSDN ?: Operator:

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.

Cuestiones relacionadas