Compiler error or code error?

Asked

Viewed 1,533 times

2

I came across the following case and do not know if it is . NET error or error in my implementation, same syntax (see image below).

Visual Studio

condenacaoInsert.qtd_ano_pena = null;
condenacaoInsert.qtd_ano_pena = (!string.IsNullOrEmpty(readerCondenacao["qtd_ano_pena"].ToString())) ? int.Parse(readerCondenacao["qtd_ano_pena"].ToString()) : null;
condenacaoInsert.qtd_mes_pena = (!string.IsNullOrEmpty(readerCondenacao["qtd_mes_pena"].ToString())) ? int.Parse(readerCondenacao["qtd_ano_pena"].ToString()) : null;
condenacaoInsert.qtd_dia_pena = (!string.IsNullOrEmpty(readerCondenacao["qtd_dia_pena"].ToString())) ? int.Parse(readerCondenacao["qtd_ano_pena"].ToString()) : null;

Error message:

Error 1 Type of conditional Expression cannot be determined because there is no implicit Conversion between 'int' and ''(...)

1 answer

4


It is extremely rare for you to encounter an error in the compiler. Never consider this until you have a very strong reason. How many times have you found a real compiler error. In dozens of languages I’ve programmed in over 30 years, some with not very good compilers, I found just over 2 or 3 compiler errors and nothing serious. And look which programming languages are my favorite subject in computing. Of course I’m not counting on mistakes I found in my compilers :)

In this case you are returning either an integer (first result of the condition) or a null. This is very explicit in the code.

Cannot a variable be integer or null. qtd_ano_pena must be the type int. The guy int does not accept null values. Either you have to make sure that the result gives an integer, a zero, for example, if it is a suitable value, or you have to change the type of this variable. Can change to a int? which is an integer type that accepts nulls. its name is integer nullable. Of course it would be good to understand all its working because you may have other problems elsewhere if you use it.

After the comment below the author I realized that one is missing cast explicit in the first expression, since it results in integer and you want an voidable integer, then the expression should put an (int?). Then you could do so much:

condenacaoInsert.qtd_ano_pena = 
     (!string.IsNullOrEmpty(readerCondenacao["qtd_ano_pena"].ToString())) ?
     (int?)int.Parse(readerCondenacao["qtd_ano_pena"].ToString()) : null;

how much

condenacaoInsert.qtd_ano_pena = 
     (!string.IsNullOrEmpty(readerCondenacao["qtd_ano_pena"].ToString())) ? 
     int.Parse(readerCondenacao["qtd_ano_pena"].ToString()) : (int?)null;

I put in the Github for future reference.

  • Dear, forgive my omission, in this case "condemnedInsert.qtd_ano_pena" is already type "int?" as in the other "qtds". Note that in the line above, I put as example "condemnoInsert.qtd_ano_pena = null", demonstrating that it is possible to accept null for the same field.

  • @Santanafire edited p/ resolve, I would put the whole line if you had posted the code and the error. Prefer to post the code and error in text, it is easier for us to read and manipulate. A screenshot can be auxiliary but not in the text link. You can still do this, edit there.

  • Dear Bigown!!! Perfect, the solution was to include the explicit cast! Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.