[Mono-devel-list] DataSetHelper Class and Mono problems?

Jonathan Pryor jonpryor at vt.edu
Fri Oct 15 07:25:49 EDT 2004


On Thu, 2004-10-14 at 23:09, Jon Benson wrote:
<snip/>
> However I run in to a problem with one of the functions throwing an
> exception under Mono.  I've created a bit of test code to reproduce this
> exception:
> Unhandled Exception: System.InvalidCastException: Cannot cast from
> source type to destination type.
> in <0x00085> Test.TestClass:Add (object,object)
> in <0x0004e> Test.TestClass:Main (string[])

You're getting an exception because your code is buggy. :-)

You're passing boxed ints to Add, but Add casts them to decimal.  A
boxed int can't be cast to a decimal.  For example:

	object boxed_int = 42;
	int i = (int) 42; // unbox: valid
	decimal d = 42;   // calls Decimal.implicit operator Decimal (int)
	decimal d2 = (decimal) boxed_int;
		// runtime cast check.  
		// boxed_int.GetType() != typeof(Decimal), so an 
		// InvalidCastException is thrown.

The last cast is precisely what your program does.  As Umadevi
mentioned, it also fails on .NET.

It fails because runtime casts don't look for any type conversion
operators that the types define, such as "implicit operator Decimal (int
value)''.  Such operator functions calls are generated by the compiler,
not the runtime.  Thus, to convert a boxed int into a decimal, you'd
need two casts:

	decimal d = (decimal) (int) boxed_int;

> Sample code to reproduce the exception:
> using System;
> 
> namespace Test
> {
>    class TestClass
>    {
>       [STAThread]
>       static void Main(string[] args)
>       {
>          int i1 = 34; int i2 = 8;
> 
>          int i3 = (int)((decimal)i1 + (decimal)i2);
>          Console.WriteLine(i3.ToString());
> 
>          i3 = (int)(TestClass.Add(i1, i2));

Notice that i1 and i2 are of type int, so  you're passing boxed ints.

>          Console.WriteLine(i3.ToString());
>       }
> 
>       private static object Add(object a, object b)
>       {
>           //Adds two values - if one is DBNull, then returns the other
>           if (a is DBNull)
>               return b;
>           if (b is DBNull)
>               return a;
>           return ((decimal)a + (decimal)b);

Here you cast the boxed ints to a decimal.  Thus, an
InvalidCastException.

>       }
>    }
> }

 - Jon





More information about the Mono-devel-list mailing list