0

I don't understand. Why does my code give an error? Is it because of var? But why?

just please don't decide for me, but explain what my mistake is and why it doesn't work. Because it's a practice and I want to do it myself. (If anything when entering 7, my code should output 12*45*7 ) https://code.sololearn.com/coEF7KRdp5uG/?ref=app

28th Jul 2022, 9:56 AM
Иван Литовченко
8 Réponses
+ 2
The use of `var` allows automatic variable type deduction, compiler decides which type to use by checking the type of data that is to be assigned for the variable. But in this case, deduction fails because there are two possibilities of data type, a string and an int. And there is no implicit conversion available for the two types involved, so an explicit type conversion is necessary..
28th Jul 2022, 3:46 PM
Ipang
+ 1
It is working fine because there are two independent blocks, the `if` block and the `else` block. + A block is a section of code wrapped in a pair of curly brackets. It can contain code or be empty e.g. {} Each block has their own definition for variable <y>. The definition of <y> in `if` block defines a string variable, while the definiton of <y> in `else` block defines an int variable. Since variable <y> is defined inside the `if` and `else` block, variable <y> will be valid only inside the respective block. And in each block, type of variable <y> is clear cause the data to be assigned for <y> is either string or int, no conditionals evaluation involved. + Implicit conversion is done by the machine, it is done when it appears necessary for the machine to continue working. + Explicit conversion is done by the coder, either by type casting operator, or by a method that generates a new type that was expected. Coder explicitly tell machine where and when the conversion should take place.
29th Jul 2022, 4:52 AM
Ipang
0
The problem was because of the ternary operation at line 17. C# cannot process that instruction because the values returned from the operation were incompatible, and there's no implicit conversion available between a string and an int. You can make this work by converting <x> into a string. This way both expressions returned by ternary operation at line 17 will have similar types (both are string). var y = ( x % 3 == 0 ) ? "*" : x.ToString(); Additionally, the loop condition at line 15 should specify <x> <= <number> in order to also include <number> in the loop range. for( int x = 1; x <= number; x++ )
28th Jul 2022, 10:33 AM
Ipang
0
Ipang But why can't I specify two different data types if the y data type is specified automatically. That is, if y gets two, then she will have an int type, and if y gets *, then she will have a string type? I do not understand
28th Jul 2022, 2:21 PM
Иван Литовченко
0
Ipang Then why does this code work? And I don't understand what explicit and implicit conversion is. https://code.sololearn.com/clTSBv9hPkNC/?ref=app
28th Jul 2022, 5:31 PM
Иван Литовченко
0
Ipang I seem to understand, thanks for the answers
29th Jul 2022, 10:44 AM
Иван Литовченко
0
Instead of the ternary operator, use: if (x%3==0) { var y = "*"; Console.Write(y); } else { var y = x; Console.Write(y) }
29th Jul 2022, 4:36 PM
Oriol
0
But keep the loop!
29th Jul 2022, 4:38 PM
Oriol