How do you assign "null" to a Delphi.net Nullable

1.6k views Asked by At

How do you assign null to a Delphi.net Nullable? I have a field which previously contained a value , I need to clear its value back to null.

depth : Nullable<Double>;

Procedure ClearDepth;
begin 
    depth := Nil;
end;

The above creates the error Error: E2010 Incompatible types : 'Nullable<System.Double>' and 'Pointer'. using Null in place of Nil gives the same error with Variant in place of Pointer. I wondered if I should be using a special constructor to generate a null nullable but I couldn't see one in the documentation?

The following works but doesn't seem like a great solution, can anyone suggest a better way?

Procedure ClearDepth;
var
    uninitialisedValue : Nullable<Double>;
begin 
    depth := uninitialisedValue;
end;
2

There are 2 answers

5
David Heffernan On

You need this syntax:

depth := Default(Nullable<Double>);
2
svick On

The type Nullable<T> is a struct. That means it has a public parameterless constructor. In the case of this type, that's the value that represents null. In C#, you would use:

new Nullable<Double>()

Another way to get the same value in C# would be

default(Nullable<Double>)

In both cases, I don't know the syntax for Delphi. In the second case, I don't know whether something like this can be even represented there.

EDIT: Apparently, you can use the second version in Delphi, but not the equivalent of the first one. That's quite surprising to me.