Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to cast object of type 'System.Single' to type 'System.Int32' [duplicate]

Tags:

c#

I am confused by getting the following error:

Unable to cast object of type 'System.Single' to type 'System.Int32'.

I am fetching data and iterating them as follows:

public MyFault
{

   public int fault_throw { get; set; }

}

 protected List<MyFault> myfaults = new List<MyFault>();
 foreach (var package in packages2)
    {
       // the following line throws an error
      myfaults.Add(new MyFault {fault_throw=(int)(package["fault_throw"])});
    }
like image 644
casillas Avatar asked May 03 '15 03:05

casillas


1 Answers

System.Single is a single-precision floating-point number. And I think it is boxed as object in package["fault_throw"]. And you can not unbox a float to an int. You can use the Convert.ToInt32() Method, if you want to convert a boxed float to an integer.

like image 194
Issa Jaber Avatar answered Nov 07 '22 07:11

Issa Jaber