Wiki copymirror

This is a mirror of the wiki at pascal.chat. Editing, history and sign-in are there.

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.

pascal

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.

Revision 1, last changed by WikiArchive .