2

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
  • Which Delphi.net? Delphi Prism? Commented Oct 18, 2011 at 14:46
  • @springy76 This looks like Delphi.net rather than Prism because Prism has nullable baked into the language. Commented Oct 18, 2011 at 14:47

2 Answers 2

3

You need this syntax:

depth := Default(Nullable<Double>);
Sign up to request clarification or add additional context in comments.

5 Comments

perfect, thanks. Should've just tried it to see what happened!
@Thom Out of interest, do both of these versions work? I have to confess to never having used delphi.net!
The first works but not the second (DEFAULT standard function expects a type identifier)
@Thom Thanks. Answer updated. Looks pretty verbose when compared to Prism or C#.
Ugh I spoke too soon. That line compiles, but is apparently ignored at runtime. When will I learn, compiling != correct...
1

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.

2 Comments

Unfortunately I can't see how to do either in Delphi. Nullable<Double>.Create(1.0) (Delphi constructor syntax) works fine, but Nullable<Double>.Create() gives an error about no overload with that number of parameters, suggesting the default constructor isn't exposed :(
update - sorry I was wrong, even Default(T) doesn't work in Delphi (it compiles, but is completely ignored at runtime, leaving depth with its previous value).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.