Mohamed Elsayed Mohamed Elsayed - 1 month ago 9
C# Question

cannot implicitly convert type 'float' to 'int'

When I try to run the following code it gives an error which is " Cannot implicitly convert type 'float' to 'int' ".

I have done a lot of searches, and sadly I wasn't able to find any help for such an error.

int sum1, cols, rows;

float h, twoEnds, x;

sum1 = (h - ((cols) * x) + twoEnds)) / (cols + 1);

Answer

The errors puts it clear

cannot implicitly convert type 'float' to 'int'

So you have a float result which can't be converted implictly to sum1 which is of type int. Try converting/casting it explicitly:

 sum1 = (int) ((h - ((cols) * x) + twoEnds)) / (cols + 1));