Can I enumerate the constants(const) from a class?
I have tried
MyClass = class
const
c1 = 'c1';
c2 = 'c2';
c3 = 'c3';
end;
procedure GetConst();
var
ctx: TRttiContext;
objType: TRttiType;
field: trttifield;
s: string;
begin
ctx := TRttiContext.Create;
objType := ctx.GetType(MyClass.ClassInfo);
for field in objType.GetDeclaredFields do
s:= field.Name;
end;
I would like to get c1, c2, c2.
Is this possible?
edit: what I want to do is define some keys for some external symbols(for a cad program)
symbol1=class
const
datafield1='datafield1';
datafield2='datafield2';
end;
symbol2=class
const
datafield21='datafield21abc';
datafield22='datafield22abc';
end
I don't like to use fields for this because I prefer not to seperate declareration and initialization. I can't use an enum since I can't define the value as a string.
TypInfo
to youruses
clause and then useGetEnumName
andGetEnumValue
to translate between the string and ordinal values. – PromiseGetEnumName
translates theint
to astring
.GetEnumValue
translates astring
to the enumint
. So even though an enum is a series ofint
s, you can use them in your code like strings. – Promise