How to use nullable types
Here is a small example of how to use the nullable types in Delphi mode ( {$mode Delphi} ). The unit's nullable source code is referenced on the nullable types wiki page.
program temperatureproject;
{$mode delphi}{$H+}
uses Nullable, SysUtils;
type
NullableFloat = TNullable<single>;
var
weekDayTemperature : array[1..7] of NullableFloat;
max, min :NullableFloat;
i, values,code:integer;
sum, temp:single;
s:string;
begin
for i:= 1 to 7 do weekDayTemperature[i].Clear;
min.Clear;
max.Clear;
// ...
for i:= 1 to 7 do
begin
writeln('Enter the temperature on '+ DefaultFormatSettings.LongDayNames[i] );
readln(s);
Val (s,temp,code);
if code = 0 then weekDayTemperature[i].SetValue(temp);
end;
// ...
sum := 0;
values := 0;
for i:= 1 to 7 do
begin
if weekDayTemperature[i].HasValue then
begin
if min.HasValue then
begin
if min.GetValue > weekDayTemperature[i].GetValue
then min.SetValue( weekDayTemperature[i].GetValue);
end
else min.SetValue( weekDayTemperature[i].GetValue);
if max.HasValue then
begin
if max.GetValue < weekDayTemperature[i].GetValue
then max.SetValue( weekDayTemperature[i].GetValue);
end
else max.SetValue( weekDayTemperature[i].GetValue);
inc(values);
sum := sum + weekDayTemperature[i].GetValue;
end;
end;
if min.HasValue then
begin
writeln('The average week temperature was ',sum/values:0:1,' degrees.');
writeln('The minimum temperature was ',min.GetValue:0:1,' degrees.');
writeln('The maximum temperature was ',max.GetValue:0:1,' degrees.');
end;
//...
writeln('Press any key to continue!');
readln;
end.See also
From the Free Pascal wiki, page "How to use nullable types", as it stood on 2022-08-06 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.
Edited on wiki.freepascal.org by 3 people, 2019–2022
From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.
| When | Who | What changed |
|---|---|---|
| Djzepi | Created page | |
| Djzepi | ||
| Bi0T1N | Fix wording as the other page does not list the code but a URL | |
| Kai Burghardt | resolve Category: Category:Pages using deprecated enclose attributes (minor) |
Talk about this page
Nobody has opened a bench for this page yet. Sign in to open one.
