Putting an object into a cell that is Single.NaN

dear support,

I am getting a system crash with the below VB .NET code
Dim s1 As Single = Single.NaN
Dim o1 As Object = s1
Dim x As New Aspose.Cells.Workbook
x.Worksheets(0).Cells(0, 0).PutValue(o1)

This crashes, but when I try to put Double.NaN, then it works
THIS WORKS
Dim s1 As Double = Double.NaN
Dim o1 As Object = s1
Dim x As New Aspose.Cells.Workbook
x.Worksheets(0).Cells(0, 0).PutValue(o1)

Also, the below also works
Dim x As New Aspose.Cells.Workbook
x.Worksheets(0).Cells(0, 0).PutValue(Single.NaN)

Can you please advise, I am putting values in a spreadhseet, but I dont know in advance the type, hence I am using objects.

thank you
guido

@gdebouver

The issue you’re encountering with Single.NaN crashing when you try to put it into a cell using Aspose.Cells is likely due to how the library handles different numeric types. While Double.NaN works without issues, Single.NaN seems to cause a problem when wrapped in an Object.

To avoid this crash, you can check the type of the value before putting it into the cell. If it’s a Single and is NaN, you can handle it differently, such as converting it to a string or using a placeholder value. Here’s a modified version of your code:

Dim s1 As Single = Single.NaN
Dim o1 As Object

If Single.IsNaN(s1) Then
    o1 = "NaN" ' or any placeholder value you prefer
Else
    o1 = s1
End If

Dim workbook As New Aspose.Cells.Workbook()
workbook.Worksheets(0).Cells(0, 0).PutValue(o1)

This approach allows you to safely handle Single.NaN without causing a crash. If you need to maintain the numeric type, consider using Double.NaN instead, as it works without issues.

@gdebouver,

It seems that when a Single is passed as an object, the runtime might select the object overload and potentially cast it to known types, which could result in a crash or exception. It appears that Single values are not managed safely by the PutValue(object) method. To address this issue, you could consider requesting Aspose.Cells to handle the type conversion when inserting the Single object value. You may want to use the appropriate overload of the PutValue() method, which includes a second Boolean parameter, “isConverted.” Setting this parameter to “True” will ensure that the Single object value is converted accurately.

Dim s1 As Single = Single.NaN
Dim o1 As Object = s1
Dim x As New Aspose.Cells.Workbook
x.Worksheets(0).Cells(0, 0).PutValue(o1, True)

Alternatively, you may do type casting manually while inserting into the worksheet cell.

Dim s1 As Single = Single.NaN
Dim o1 As Object = s1
Dim x As New Aspose.Cells.Workbook
If TypeOf o1 Is Single Then
     x.Worksheets(0).Cells(0, 0).PutValue(CDbl(CType(o1, Single)))
Else
     x.Worksheets(0).Cells(0, 0).PutValue(o1)
End If

Hope, this helps a bit.