Difference between revisions of "Runtime Type Information (RTTI)"

From Lazarus wiki
Jump to navigationJump to search
Line 21: Line 21:
 
end.
 
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
But you can also do it without RTTI:
 +
<syntaxhighlight>
 +
program noRTTI;
 +
type
 +
  TProgrammerType = (tpDelphi, tpVisualC, tpVB, tpJava) ;
 +
var
 +
  s: string;
 +
begin
 +
  writestr(s,tpDelphi);
 +
  writeln(s);
 +
end.</syntaxhighlight>
  
 
==See Also==
 
==See Also==

Revision as of 16:53, 19 September 2016

English (en) français (fr) русский (ru)

Runtime Type Information RTTI can be utilized to obtain meta information in a Pascal application.

Converting a enumerated type to a string

One can use RTTI to obtain a string from a enumerated type.

uses TypInfo; 

type
  TProgrammerType = (tpDelphi, tpVisualC, tpVB, tpJava) ;

var 
  s: string;
begin
  s := GetEnumName(TypeInfo(TProgrammerType), integer(tpDelphi));
  // Here s = 'tpDelphi'
  Writeln(s)
end.

But you can also do it without RTTI:

program noRTTI;
type
  TProgrammerType = (tpDelphi, tpVisualC, tpVB, tpJava) ; 
var 
  s: string;
begin
  writestr(s,tpDelphi);
  writeln(s);
end.

See Also