6

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"])});
    }
casillas
  • 16,351
  • 19
  • 115
  • 215

3 Answers3

8

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.

Issa Jaber
  • 399
  • 2
  • 12
  • 1
    Thanks Issa, what should I use whenI change from `int` to `float` as follows `public float fault_throw { get; set; }` – casillas May 03 '15 at 04:11
  • It is giving error. Therefore, I have tried to use `Convert.ToSingle()` and it works. – casillas May 03 '15 at 04:15
  • which type ist package["fault_throw"] ? – Issa Jaber May 03 '15 at 04:16
  • 2
    _"you cant cast a floating-point number to an Integer"_ -- sure you can. What you can't do is unbox a `float` as an `int`. The OP unfortunately did not provide a good enough code example, but based on the reported error, I assume that `package["fault_throw"]` returns the type `object`. If it returned the type `float`, the cast would work. – Peter Duniho May 03 '15 at 04:29
  • float s = 2.8F; object f = s; int a = Convert.ToInt32(f); ... it works – Issa Jaber May 03 '15 at 04:54
  • 2
    _"float s = 2.8F; object f = s; int a = Convert.ToInt32(f); ... it works"_ -- I never said it didn't. I'm taking issue with your use of the word "must". I see though that you have edited your answer to address that error as well. Thank you for correcting that problem. – Peter Duniho May 03 '15 at 05:12
3

Without a good, minimal, complete code example that reliably reproduces the problem, it's impossible to know for sure what the problem in your code is.

However, based on the error message it seems likely that the expression package["fault_throw"] returns type object, and contains a boxed instance of a float value.

Under that assumption, all you really need is to unbox the value correctly, and then cast it. E.g. (int)(float)package["fault_throw"].

Now, that said: if you don't know in advance what the actual type of the value returned by the expression package["fault_throw"] is, then the Convert.ToInt32() method would be appropriate. It can handle a variety of inputs (including string) and it will do whatever it can to convert the value to an int.

But if you know for sure the value is a float (i.e. System.Single), then it would be more efficient to just unbox the value correctly and cast it (as my example above).

Community
  • 1
  • 1
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
0

Have you tried CInt() or Integer.Parse Maybe Convert.ToInt32() can work too.

Abhishek Dey
  • 1,601
  • 1
  • 15
  • 38