views:

255

answers:

1

We have code that casts an object to a short.

type.BusinessAreaID = (short)caType.credit_assessment_biz_areaReference.EntityKey.EntityKeyValues[0].Value;

The object has the value of 2.

This code (unit test) works on the PC of the developer that created the code. But we get a invalid cast exception on the build server and on another dev PC.

The BusinessAreaID is of type short.

The Dll is also in the GAC, we have updated that so that it should be the same.

Anyone have any ideas.

+4  A: 

You can't unbox a value to a different type. For example, this works:

short x = 2;
object y = (object)x;
short z = (short)y;

but this does not:

int x = 2;
object y = (object)x;
short z = (short)y; // InvalidCastException

Are you sure the value stored in caType...lues[0].Value is of type short?

dtb
Value is of type object
Shiraz Bhaiji
That's the type of the property. But as you can see in my example, you can store a value of type `short` in a variable of type `object`.
dtb