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

From Lazarus wiki
Jump to navigationJump to search
m (Fixed syntax highlighting)
 
(One intermediate revision by one other user not shown)
Line 2: Line 2:
  
 
'''Runtime Type Information RTTI''' can be utilized to obtain meta information in a Pascal application.
 
'''Runtime Type Information RTTI''' can be utilized to obtain meta information in a Pascal application.
 +
 +
 
__TOC__
 
__TOC__
 +
  
 
==Converting a enumerated type to a string==
 
==Converting a enumerated type to a string==
 
One can use RTTI to obtain a string from a enumerated type.
 
One can use RTTI to obtain a string from a enumerated type.
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
uses TypInfo;  
 
uses TypInfo;  
  
Line 18: Line 21:
 
   s := GetEnumName(TypeInfo(TProgrammerType), integer(tpDelphi));
 
   s := GetEnumName(TypeInfo(TProgrammerType), integer(tpDelphi));
 
   // Here s = 'tpDelphi'
 
   // Here s = 'tpDelphi'
   Writeln(s)
+
   WriteLn(s)
 
end.
 
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
But you can also do it without RTTI:
 
But you can also do it without RTTI:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
program noRTTI;
 
program noRTTI;
 
type
 
type
Line 30: Line 34:
 
   s: string;
 
   s: string;
 
begin
 
begin
   writestr(s,tpDelphi);
+
   writestr(s, tpDelphi);
   writeln(s);
+
   WriteLn(s);
 
end.</syntaxhighlight>
 
end.</syntaxhighlight>
  

Latest revision as of 13:44, 25 February 2020

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