Is there an existing issue for this?
Describe the bug
Using double? for two way binding in BitNumberField results in exception.
Expected Behavior
I did not find anywhere in documentation that nullable doubles should not work, so I am assuming it is a bug, since int? works and is also shown in documentation.
Steps To Reproduce
@page "/bug"
<BitNumberField @bind-Value=_nullableDouble />
@code {
double? _nullableDouble = 1;
}
this example will throw on load
Exceptions (if any)
System.InvalidCastException: Invalid cast from 'System.Double' to 'System.Nullable`1[[System.Double, System.Private.CoreLib, Version=10.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]'.
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
at Bit.BlazorUI.BitNumberField`1.Normalize(TValue value) in C:\Users\Jakub\source\repos\bitplatform\src\BlazorUI\Bit.BlazorUI\Components\Inputs\NumberField\BitNumberField.razor.cs:line 688
at Bit.BlazorUI.BitNumberField`1.NormalizeValue() in C:\Users\Jakub\source\repos\bitplatform\src\BlazorUI\Bit.BlazorUI\Components\Inputs\NumberField\BitNumberField.razor.cs:line 729
at Bit.BlazorUI.BitNumberField`1.OnInitializedAsync() in C:\Users\Jakub\source\repos\bitplatform\src\BlazorUI\Bit.BlazorUI\Components\Inputs\NumberField\BitNumberField.razor.cs:line 318
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
at Bit.BlazorUI.BitNumberField`1.SetParametersAsync(ParameterView parameters) in C:\Users\Jakub\source\repos\bitplatform\src\BlazorUI\Bit.BlazorUI\obj\Debug\net10.0\Bit.BlazorUI.SourceGenerators\Bit.BlazorUI.SourceGenerators.Component.ComponentSourceGenerator\BitNumberField_SetParametersAsync.AutoGenerated.cs:line 295
.NET Version
10.0.201
Anything else?
It looks like the issue is in calling Convert.ChangeType(...) in
|
return (TValue)Convert.ChangeType(Math.Round(doubleValue, _precision), typeof(TValue)); |
as the typeof(TValue) will return the nullable type instead of the underlying type. Using
_typeOfValue looks like it fixes the issue.
private TValue Normalize(TValue value)
{
if (value is double doubleValue)
{
return (TValue)Convert.ChangeType(Math.Round(doubleValue, _precision), _typeOfValue);
}
else if (value is float floatValue)
{
return (TValue)Convert.ChangeType(Math.Round(floatValue, _precision), _typeOfValue);
}
else if (value is decimal decimalValue)
{
return (TValue)Convert.ChangeType(Math.Round(decimalValue, _precision), _typeOfValue);
}
return value;
}
Is there an existing issue for this?
Describe the bug
Using
double?for two way binding inBitNumberFieldresults in exception.Expected Behavior
I did not find anywhere in documentation that nullable doubles should not work, so I am assuming it is a bug, since
int?works and is also shown in documentation.Steps To Reproduce
this example will throw on load
Exceptions (if any)
.NET Version
10.0.201
Anything else?
It looks like the issue is in calling
Convert.ChangeType(...)inbitplatform/src/BlazorUI/Bit.BlazorUI/Components/Inputs/NumberField/BitNumberField.razor.cs
Line 771 in ff7da72
_typeOfValuelooks like it fixes the issue.