Advertisement

C# problems when casting a 0 to a data type

Started by March 14, 2003 01:01 AM
4 comments, last by IFooBar 21 years, 8 months ago
Hello I keep on getting an illegal cast exception when I try to cast an variable of type object (which has a 0 in it) to a float or an int or a double or anythign else. You see I have a class called DynamicArray and There is a delegate that takes in two objects and returns an int based on whether object one is less then, equal, or greater then object two. I have my CompareFunction set up like this
  
public int Cmp( object one, object two )
{
    float a = (float)one;
    float b = (float)two;

    if( a > b ) return 1;
    else if ( a < b ) return -1;
    else return 0;
}
  
The problem is when I use this function in my SearchFunction(). (all the search function does is go through the array elements and check if Cmp( arayy, objectToBeFound ) == 0. If it does equal 0 then the object has been found and it returns success. When Cmp() is invoked by LinearSearch() I get the exception. Anyone know how I can fix this? Oh and I only get the exception when I try to look for a 0. So doing this would work fine: DynamicArray.LinearSearch( 3.5f ); but doing this would throw an exception DynamicArray.LinearSearch( 0 ); thanks for any help :::: [ Triple Buffer V2.0 ] ::::
[size=2]aliak.net
quote: Original post by IFooBar
DynamicArray.LinearSearch( 0 );

Try DynamicArray.LinearSearch( 0f );




"If there is a God, he is a malign thug."
-- Mark Twain
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Advertisement
Yes that works. But I still dont understand why the casting dosnt work. I assume that once I pass in ''0'' as an object, the compiler takes it as an int? if that is true then there shouldnt be any problems converting an int to a float. Is there a way to force a cast (even if it could be dangerous)?

thanks for your reply.
[size=2]aliak.net
quote: Original post by IFooBar
Yes that works. But I still dont understand why the casting dosnt work. I assume that once I pass in ''0'' as an object, the compiler takes it as an int?

Yes - a literal 0 is of type int.
quote:
if that is true then there shouldnt be any problems converting an int to a float.

Theres more going on here than a simple cast. When you do float a = (float)one;, you invoke an unboxing operation. This won''t work unless the boxed object is the exact type you are casting to. I suggest you read up on boxing/unboxing.
quote:
Is there a way to force a cast (even if it could be dangerous)?

Wouldn''t really be any point - a boxed int is fundamentally different from a boxed float. You could always do float a = (float)((int)one);, but of course, that would fail if one actually was a float.


"If there is a God, he is a malign thug."
-- Mark Twain
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
quote: Original post by IFooBar
Is there a way to force a cast (even if it could be dangerous)?

float a = System.Convert.ToSingle(one);

Will convert any object that implements the IConvertible interface. Fortunately this includes all the primitive types, including the int type that seems to be giving you trouble.
thanx a bunch guys. I guess i should also read up on un/boxing.
[size=2]aliak.net

This topic is closed to new replies.

Advertisement