Cast object to decimal? (nullable decimal)

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 … Read more

SparkSQL: How to deal with null values in user defined function?

This is where Optioncomes in handy: val extractDateAsOptionInt = udf((d: String) => d match { case null => None case s => Some(s.substring(0, 10).filterNot(“-“.toSet).toInt) }) or to make it slightly more secure in general case: import scala.util.Try val extractDateAsOptionInt = udf((d: String) => Try( d.substring(0, 10).filterNot(“-“.toSet).toInt ).toOption) All credit goes to Dmitriy Selivanov who’ve pointed … Read more

Nullable values in C++

Boost.Optional probably does what you need. boost::none takes the place of your CNullValue::Null(). Since it’s a value rather than a member function call, you can do using boost::none; if you like, for brevity. It has a conversion to bool instead of IsNull, and operator* instead of GetValue, so you’d do: void writeToDB(boost::optional<int> optional_int) { if … Read more

Why do nullable bools not allow if(nullable) but do allow if(nullable == true)?

There’s no implicit conversion from Nullable<bool> to bool. There is an implicit conversion from bool to Nullable<bool> and that’s what happens (in language terms) to each of the bool constants in the first version. The bool operator==(Nullable<bool>, Nullable<bool> operator is then applied. (This isn’t quite the same as other lifted operators – the result is … Read more

TryGetValue pattern with C# 8 nullable reference types

If you’re arriving at this a little late, like me, it turns out the .NET team addressed it through a bunch of parameter attributes like MaybeNullWhen(returnValue: true) in the System.Diagnostics.CodeAnalysis space which you can use for the try pattern. Returning a swift-style nullable reference type works well, but the try pattern lets you return things … Read more

C# 4: Dynamic and Nullable

Great question. Two facts that you probably don’t know: Dynamic behind the scenes is just object. That is, a “dynamic” variable is an “object” variable with a hint to the compiler that says “generate dynamic operations on this variable when it is used.” There is no such thing as a boxed nullable. When you box … Read more

How does GetValueOrDefault work?

thing isn’t null. Since structs can’t be null, so Nullable<int> can’t be null. The thing is… it is just compiler magic. You think it is null. In fact, the HasValue is just set to false. If you call GetValueOrDefault it checks if HasValue is true or false: public T GetValueOrDefault(T defaultValue) { return HasValue ? … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)