Why does my converter giving an invalid cast error? Why does my converter giving an invalid cast error? wpf wpf

Why does my converter giving an invalid cast error?


You are attempting to cast (not convert) from double to int, which won't work. You need to do an implicit conversion or use Convert.ToInt32() -- since the argument is actually of type object I think you'll need the latter to keep the compiler happy. It's up to you whether you want to include the culture's format provider or not.

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture){    return Convert.ToInt32(value);}

You can use the cast operator when the object's have the same shape, that is, when one object is an instance of the type to which you are casting. For example, if class Foo extends class Bar, then you can cast an object of type Foo to type Bar - because a Foo object has all the methods and properties that a Bar object would. You couldn't, however, cast an object of type Bar to type Foo because Foo changes (or can change, as far as the compiler is concerned) the shape of Bar, by adding methods or properties that a Bar object doesn't have.

In your case you are dealing with primitive types which only share the object interface - there isn't an inheritance relationship between them except that they both derive from object. There is, however, an implicit conversion between the two. You can assign an object one type to a variable of the other, though you may lose some precision in the value.

double x = 1.1;int y = 0;y = x;  // implicit conversion, this works, y will be 1x = y;  // implicit conversion, this works, x will be 1.0

You can't, however, cast an object of one type to the other. Casting implies that you will be using the object as if it were of the other type. In this case the shapes differ and it can't be done.


The problem is that you are attempting to do both an unbox and a cast at the same time. This will fail. You must first unbox and then cast to the appropriate type.

return (int)(double)value;

Eric Lippert Recently wrote a nice article on exactly why this is necessary. It's worth the read


The double value is boxed inside an object, and the only way to get it out is to unbox it as a double. After that you can cast it to an int.

return (int)(double)value;

You can also use the Convert.ToInt32(object) method (as tvanfosson suggested), which will cast the object to IConvertible and call it's virtual ToInt32 method, which in turn will call the Convert.ToInt32(double) method. That is of course a bit more overhead.