Cast object to decimal? (nullable decimal)
Asked Answered
C

6

35

If have this in the setter of a property:

decimal? temp = value as decimal?;

value = "90"

But after the cast, temp is null...

What is the proper way to do this cast?

Cambist answered 9/2, 2009 at 8:59 Comment(0)
I
70

Unboxing only works if the type is identical! You can't unbox an object that does not contain the target value. What you need is something along the lines of

decimal tmpvalue;
decimal? result = decimal.TryParse((string)value, out tmpvalue) ?
                  tmpvalue : (decimal?)null;

This looks whether the value is parsable as a decimal. If yes, then assign it to result; else assign null. The following code does approximately the same and might be easier to understand for people not familiar with the conditional operator ?::

decimal tmpvalue;
decimal? result = null;
if (decimal.TryParse((string)value, out tmpvalue))
    result = tmpvalue;
Ingeingeberg answered 9/2, 2009 at 9:2 Comment(5)
I wouldn't use the word "identical" here. For instance, you can unbox between enum types and their underlying type, T and T?, and some other odd cases IIRC. The CLR is more permissive than one might expect.Analyse
(But yes, you shouldn't expect unboxing to parse a string :)Analyse
@Jon: have you got a better formulation? Lacking that, I'm going to copy your comment into my answer because it expresses the caveat nicely.Ingeingeberg
Actually, Parse with one argument is a shortcut for TryParse with style and culture with default arguments.. There is no one to one mapping between string and primitive types, so there's no cast.Mozzetta
The case of enums is the only one where you can unbox to another type. Other unboxing simply get the object'ed value and assign it to the variable on the stack. No other conversion occures.Mozzetta
M
6

you should parse the decimal. But if you want your decimal to be null when the string is not correct, use TryParse :

decimal parsedValue;
decimal? temp = decimal.TryParse(value, out parsedValue)
                ? value
                : (decimal?)null;

This way you will avoid exceptions while parsing ill formated strings.

Almost all primitive types provide a Parse and TryParse methods to convert from string.

Is is also recommended to pass a culture for the provider argument to the method to avoid problems with the decimal separator. If you're reading from another system, CultureInfo.InvariantCulture is probably the way to go (but it's not the default).

bool TryParse(string s, NumberStyles style,
  IFormatProvider provider, out decimal result)
Mozzetta answered 9/2, 2009 at 9:6 Comment(2)
I had forgotten the (decimal?) cast for null... sorry, but the culture part is still important.Mozzetta
This answer is incorrect as assigning 'value' to 'temp' will not work. It should be ? parsedValue : (decimal?)null;Foretoken
E
4

If you do not want to parse strings, but want to ensure that you receive either null, a decimal or a nullable decimal, then you could do something like this:

public static Nullable<T> Convert<T>(object input) 
    where T : struct
{
    if (input == null)
        return null;
    if (input is Nullable<T> || input is T)
        return (Nullable<T>)input;
    throw new InvalidCastException();
}

You could make it return null on the last line instead if you wished to avoid exceptions, although this would not distinguish between real null values and bad casts.

Note that you have to use the "is" operator, as the "as" operator does not work on value types, and casting without checking may thrown an InvalidCastException.

You could also make it an extension method:

public static class ObjectExtensions
{
    public static Nullable<T> ToNullable<T>(this object input)
        where T : struct
    {
        if (input == null)
            return null;
        if (input is Nullable<T> || input is T)
            return (Nullable<T>)input;
        throw new InvalidCastException();
    }
}

And use it like this:

object value = 123.45m;
decimal? dec = value.ToNullable<decimal>();

This will help avoid code contract warnings about unboxing null references.

Elasmobranch answered 28/6, 2011 at 9:46 Comment(2)
Have you tested your extension method?? I tried it and got a NullReferenceException for a valid double value that needed to be converted to nullable decimalDefamatory
@Defamatory The extension method is not intended for doubles, as it states in the first line: "that you receive either null, a decimal or a nullable decimal"Elasmobranch
B
3

and if you use decimal? temp = (decimal?)value;

Buddleia answered 9/2, 2009 at 9:2 Comment(2)
that doesn't work. I can't explain why. In debugmode, Visual Studio wants to show the disassembly...Cambist
It won't work if value holds an instance of string. It should work if it holds an instance of decimal.Selfdefense
R
2

Surprisingly, but good old System.Convert.ToDecimal(myNullableDoubleBoxedInObject) works perfectly:

decimal? myNullableDecimal = 0.15m;
object myNullableDoubleBoxedInObject = myNullableDouble ;
decimal myDecimal = System.Convert.ToDouble(myNullableDoubleBoxedInObject);
Repetend answered 12/8, 2020 at 2:0 Comment(0)
J
1

Simple object Extension will also do the trick:

public static class ObjectExtensions
{
    public static decimal ToDecimal(this object obj)
    {
        return System.Convert.ToDecimal(obj);
    }
}
decimal? number = obj?.ToDecimal();
Jaye answered 20/7, 2022 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.