ToStr: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | # About | |
| 2 | 2 | ||
| 3 | 3 | **ToStr** is helper function to convert any value to a string representation, including records and arrays, in the same spirit of 'print' functions found in scripting languages. Its usefull for logging and debugging. | |
| 4 | 4 | ||
| 5 | 5 | ``` pascal | |
| 6 | 6 | function ToStr(const AValue; ATypeInfo: PTypeInfo): AnsiString; | |
| 7 | 7 | ``` | |
| 8 | 8 | ||
| 9 | 9 | Parametes: | |
| 10 | 10 | ||
| 11 | 11 | - AValue: The variable to convert to string. | |
| 12 | 12 | - ATypeInfo: The type information returned by 'TypeInfo' compiler intrinsic. | |
| 13 | 13 | ||
| 14 | 14 | # Download | |
| 15 | 15 | ||
| 16 | 16 | You can get the code at <https://github.com/correaelias/TypeUtils> | |
| 17 | 17 | ||
| 18 | 18 | # Examples | |
| 19 | 19 | ||
| 20 | 20 | Simple example. | |
| 21 | 21 | ||
| 22 | 22 | ``` pascal | |
| 23 | 23 | program test; | |
| 24 | 24 | uses TypeUtils; | |
| 25 | 25 | var | |
| 26 | 26 | Arr: array[1 .. 3] of Integer = (1, 2, 3); | |
| 27 | 27 | begin | |
| 28 | 28 | WriteLn(ToStr(Arr, TypeInfo(Arr))); | |
| 29 | 29 | end. | |
| 30 | 30 | ``` | |
| 31 | 31 | ||
| 32 | 32 | Example converts array of record. | |
| 33 | 33 | ||
| 34 | 34 | ``` pascal | |
| 35 | 35 | program Test; | |
| 36 | 36 | ||
| 37 | 37 | uses TypeUtils, SysUtils; | |
| 38 | 38 | ||
| 39 | 39 | type | |
| 40 | 40 | TEmployee = record | |
| 41 | 41 | FName: ShortString; | |
| 42 | 42 | FId: LongInt; | |
| 43 | 43 | FSalary: Currency; | |
| 44 | 44 | end; | |
| 45 | 45 | ||
| 46 | 46 | var | |
| 47 | 47 | Employees: array of TEmployee; | |
| 48 | 48 | I: LongInt; | |
| 49 | 49 | begin | |
| 50 | 50 | SetLength(Employees, 10); | |
| 51 | 51 | ||
| 52 | 52 | for I := 0 to High(Employees) do | |
| 53 | 53 | begin | |
| 54 | 54 | with Employees[I] do | |
| 55 | 55 | begin | |
| 56 | 56 | FName := 'John Doe ' + I.ToString; | |
| 57 | 57 | FId := I; | |
| 58 | 58 | FSalary := 2000.25 * I; | |
| 59 | 59 | end; | |
| 60 | 60 | end; | |
| 61 | 61 | ||
| 62 | 62 | WriteLn(ToStr(Employees, TypeInfo(Employees))); | |
| 63 | 63 | end. | |
| 64 | 64 | ``` | |
| 65 | 65 | ||
| 66 | 66 | It shows: | |
| 67 | 67 | ||
| 68 | - | ||
| 69 | - | ||
| 70 | - | ||
| 71 | - | ||
| 68 | + | ```text | |
| 69 | + | [('John Doe 0', 0, 0), ('John Doe 1', 1, 2000.25), ('John Doe 2', 2, 4000.5), | |
| 70 | + | ('John Doe 3', 3, 6000.75), ('John Doe 4', 4, 8001), ('John Doe 5', 5, 10001.25), | |
| 71 | + | ('John Doe 6', 6, 12001.5), ('John Doe 7', 7, 14001.75), ('John Doe 8', 8, 16002), | |
| 72 | + | ('John Doe 9', 9, 18002.25)] | |
| 73 | + | ``` | |
| 72 | 74 | ||
| 73 | 75 | --- | |
| 74 | 76 | *From the Free Pascal wiki, page "ToStr", as it stood on 2022-08-26 after a change by Alextp, 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).* |
