Sign in Join
Wiki copytemporary

ToStr: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11# About
22
33**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.
44
55``` pascal
66function ToStr(const AValue; ATypeInfo: PTypeInfo): AnsiString;
77```
88
99Parametes:
1010
1111- AValue: The variable to convert to string.
1212- ATypeInfo: The type information returned by 'TypeInfo' compiler intrinsic.
1313
1414# Download
1515
1616You can get the code at <https://github.com/correaelias/TypeUtils>
1717
1818# Examples
1919
2020Simple example.
2121
2222``` pascal
2323program test;
2424uses TypeUtils;
2525var
2626 Arr: array[1 .. 3] of Integer = (1, 2, 3);
2727begin
2828 WriteLn(ToStr(Arr, TypeInfo(Arr)));
2929end.
3030```
3131
3232Example converts array of record.
3333
3434``` pascal
3535program Test;
3636
3737uses TypeUtils, SysUtils;
3838
3939type
4040 TEmployee = record
4141 FName: ShortString;
4242 FId: LongInt;
4343 FSalary: Currency;
4444 end;
4545
4646var
4747 Employees: array of TEmployee;
4848 I: LongInt;
4949begin
5050 SetLength(Employees, 10);
5151
5252 for I := 0 to High(Employees) do
5353 begin
5454 with Employees[I] do
5555 begin
5656 FName := 'John Doe ' + I.ToString;
5757 FId := I;
5858 FSalary := 2000.25 * I;
5959 end;
6060 end;
6161
6262 WriteLn(ToStr(Employees, TypeInfo(Employees)));
6363end.
6464```
6565
6666It shows:
6767
68-`[('John Doe 0', 0, 0), ('John Doe 1', 1, 2000.25), ('John Doe 2', 2, 4000.5), `\
69-`('John Doe 3', 3, 6000.75), ('John Doe 4', 4, 8001), ('John Doe 5', 5, 10001.25), `\
70-`('John Doe 6', 6, 12001.5), ('John Doe 7', 7, 14001.75), ('John Doe 8', 8, 16002), `\
71-`('John Doe 9', 9, 18002.25)]`
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+```
7274
7375---
7476*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).*

Back to history