Hi
I remember there is a way to get a string representation of an enumerated type directly without using a lookup string array, but I forgot the syntax. Lazarus uses only lookup arrays, maybe because the other syntax is new.
How is the syntax?
Juha

Mailing lists / fpc-pascal / 2011-11
9 messages · 8 people ·
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hi
I remember there is a way to get a string representation of an enumerated type directly without using a lookup string array, but I forgot the syntax. Lazarus uses only lookup arrays, maybe because the other syntax is new.
How is the syntax?
Juha
One option is RTTI:
Hi I remember there is a way to get a string representation of an enumerated type…
You can directly write enums with Write or WriteLn and convert enums to strings with WriteStr.
program EnumStr;
{$mode objfpc}{$H+}
type
TColor = (cRed, cGreen, cBlue, cYellow);
function ColorToStr(C: TColor) : String;
begin
WriteStr(Result, C);
end;
var
Color : TColor;
begin
for Color in TColor do
WriteLn(ColorToStr(Color));
end.2011/11/1 Juha Manninen :
Hi I remember there is a way to get a string representation of an enumerated type…
On Tue, November 1, 2011 08:41, Juha Manninen wrote:
Hi,
type
TNum = (One, Two, Three);
var
N: TNum;
S: string;
begin
N := One;
WriteLn (N);
N := Two;
WriteStr (S, N);
WriteLn (S);
end.Tomas
Hi I remember there is a way to get a string representation of an enumerated type…
Hi,
This is code I wrote for converting 1, 2 or 3 digit numbers to string.
function NumtoString(N:integer):string;
var
c1, c2, c3: char;
begin
if (N<10) then
NumtoString := chr(N+48)
else if (N > 9) and (N < 100) then
begin
c1 := chr((N div 10) + 48);
c2 := chr((N mod 10) + 48);
NumtoString := concat(c1, c2);
end
else if (N > 99) and (N < 1000) then
begin
c1 := chr((N div 100) + 48);
c2 := chr((N mod 100) div 10 + 48);
c3 := chr((N mod 10) + 48);
NumtoString := concat(c1, c2, c3);
end;
end;Thomas Young
cell: 330-256-7064
mobile email:
type TNum = (One, Two, Three); var N: TNum; S: string; begin N := One; WriteLn (N); N :=…
WriteStr (S, N);
Thanks guys!
WriteStr() was the function I was looking for.
I found some places in Lazarus code where it is used but not many. Martin's debugger code has some.
I think many lookup string arrays in Lazarus code could be replaces with WriteStr. It would reduce code size and improve code maintenance.
If I understand right WriteStr uses RTTI internally. So, every enum type already carries the string explicit lookup
array is duplicate data.
Juha
2011/11/1 Juha Manninen :
It is a relatively new feature, since fpc 2.4.0 IIRC.
Vincent
Thanks guys! WriteStr() was the function I was looking for. I found some places in…
You can also use the plain str() procedure.
Jonas
Thanks guys! WriteStr() was the function I was looking for. I found some places in…
Not really, if rtti for a certain enum is never used, the linker won't include it.