How to use nullable types: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: How to use nullable types
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Here is a small example of how to use the [[Nullable types|nullable types]] in [[Mode Delphi|Delphi mode ( {$mode Delphi} )]]. The [[Unit|unit's]] nullable [[Source code|source code]] is referenced on the [[Nullable types|nullable types]] wiki page. | |
| 2 | + | ||
| 3 | + | ``` pascal | |
| 4 | + | ||
| 5 | + | program temperatureproject; | |
| 6 | + | {$mode delphi}{$H+} | |
| 7 | + | uses Nullable, SysUtils; | |
| 8 | + | ||
| 9 | + | type | |
| 10 | + | ||
| 11 | + | NullableFloat = TNullable<single>; | |
| 12 | + | ||
| 13 | + | var | |
| 14 | + | weekDayTemperature : array[1..7] of NullableFloat; | |
| 15 | + | max, min :NullableFloat; | |
| 16 | + | i, values,code:integer; | |
| 17 | + | sum, temp:single; | |
| 18 | + | s:string; | |
| 19 | + | begin | |
| 20 | + | for i:= 1 to 7 do weekDayTemperature[i].Clear; | |
| 21 | + | min.Clear; | |
| 22 | + | max.Clear; | |
| 23 | + | // ... | |
| 24 | + | for i:= 1 to 7 do | |
| 25 | + | begin | |
| 26 | + | writeln('Enter the temperature on '+ DefaultFormatSettings.LongDayNames[i] ); | |
| 27 | + | readln(s); | |
| 28 | + | Val (s,temp,code); | |
| 29 | + | if code = 0 then weekDayTemperature[i].SetValue(temp); | |
| 30 | + | end; | |
| 31 | + | // ... | |
| 32 | + | sum := 0; | |
| 33 | + | values := 0; | |
| 34 | + | for i:= 1 to 7 do | |
| 35 | + | begin | |
| 36 | + | if weekDayTemperature[i].HasValue then | |
| 37 | + | begin | |
| 38 | + | if min.HasValue then | |
| 39 | + | begin | |
| 40 | + | if min.GetValue > weekDayTemperature[i].GetValue | |
| 41 | + | then min.SetValue( weekDayTemperature[i].GetValue); | |
| 42 | + | end | |
| 43 | + | else min.SetValue( weekDayTemperature[i].GetValue); | |
| 44 | + | if max.HasValue then | |
| 45 | + | begin | |
| 46 | + | if max.GetValue < weekDayTemperature[i].GetValue | |
| 47 | + | then max.SetValue( weekDayTemperature[i].GetValue); | |
| 48 | + | end | |
| 49 | + | else max.SetValue( weekDayTemperature[i].GetValue); | |
| 50 | + | inc(values); | |
| 51 | + | sum := sum + weekDayTemperature[i].GetValue; | |
| 52 | + | end; | |
| 53 | + | end; | |
| 54 | + | if min.HasValue then | |
| 55 | + | begin | |
| 56 | + | writeln('The average week temperature was ',sum/values:0:1,' degrees.'); | |
| 57 | + | writeln('The minimum temperature was ',min.GetValue:0:1,' degrees.'); | |
| 58 | + | writeln('The maximum temperature was ',max.GetValue:0:1,' degrees.'); | |
| 59 | + | end; | |
| 60 | + | //... | |
| 61 | + | writeln('Press any key to continue!'); | |
| 62 | + | readln; | |
| 63 | + | end. | |
| 64 | + | ||
| 65 | + | ``` | |
| 66 | + | ||
| 67 | + | ## See also | |
| 68 | + | ||
| 69 | + | - [[Val]] | |
| 70 | + | - [[For]] | |
| 71 | + | ||
| 72 | + | --- | |
| 73 | + | *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](/wiki-contact).* |
