Invalid cast from ‘System.Int32’ to ‘System.Nullable`1[[System.Int32, mscorlib]]

You have to use Nullable.GetUnderlyingType to get underlying type of Nullable. This is the method I use to overcome limitation of ChangeType for Nullable public static T ChangeType<T>(object value) { var t = typeof(T); if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) { if (value == null) { return default(T); } t = Nullable.GetUnderlyingType(t); } return (T)Convert.ChangeType(value, t); } … Read more

Why should casting be avoided? [closed]

You’ve tagged this with three languages, and the answers are really quite different between the three. Discussion of C++ more or less implies discussion of C casts as well, and that gives (more or less) a fourth answer. Since it’s the one you didn’t mention explicitly, I’ll start with C. C casts have a number … Read more

How should I cast in VB.NET?

Those are all slightly different, and generally have an acceptable usage. var.ToString() is going to give you the string representation of an object, regardless of what type it is. Use this if var is not a string already. CStr(var) is the VB string cast operator. I’m not a VB guy, so I would suggest avoiding … Read more

Change type of varchar field to integer: “cannot be cast automatically to type integer”

There is no implicit (automatic) cast from text or varchar to integer (i.e. you cannot pass a varchar to a function expecting integer or assign a varchar field to an integer one), so you must specify an explicit cast using ALTER TABLE … ALTER COLUMN … TYPE … USING: ALTER TABLE the_table ALTER COLUMN col_name … Read more

casting char[][] to char** causes segfault?

A char[ROWS][COLS+1] cannot be cast into a char**. The input argument of print_map should be void print_map(char map[][COLS+1]) or void print_map(char (*map)[COLS+1]) The difference being that a char** means to point to something that can be dereferenced like this: (char**)map | v +——–+——–+——+——–+– … | 0x1200 | 0x1238 | NULL | 0x1200 | +—-|—+—-|—+–|—+—-|—+– … … Read more