views:

5012

answers:

3

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?

+11  A: 

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 = tmvalue;
Konrad Rudolph
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.
Jon Skeet
(But yes, you shouldn't expect unboxing to parse a string :)
Jon Skeet
@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.
Konrad Rudolph
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.
Think Before Coding
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.
Think Before Coding
A: 

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

FailBoy
that doesn't work. I can't explain why. In debugmode, Visual Studio wants to show the disassembly...
Natrium
Thats very strange because it works fine on my side.....
FailBoy
+2  A: 

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)
Think Before Coding
I had forgotten the (decimal?) cast for null... sorry, but the culture part is still important.
Think Before Coding
This answer is incorrect as assigning 'value' to 'temp' will not work. It should be ? parsedValue : (decimal?)null;
TheJuice