I shall talk about FPC 2.6.2. First of all - there can be enumerations with predefined values. In 2.6.2, typeinfo for such enumerations does not exist. So solutions with GetEnumName are limited and cannot be used in general case.
str(MyEnum,MyString) and writestr(MyString,MyEnum) give correct results also for enumeraations with assigned values. But there are two problems. First - definitely function is much more comfortable than procedure. Second, catastrophical - enumeration variable can contain value out of definition scope (always contains it, if enumeration does not start from 0). str in such cases generates runtime error, and program is finished. writestr generates exception, which can be caught. So, my current solution - generic class (unfortunately, generic functions still not implemented in FP). It requires "specialize" statement for every enumeration we propose to convert. Definitions: type generic Enumstr<T>=class class function tostr(value:T):string; end; class function Enumstr.tostr(value: T): string; begin try writestr(result,value); except on e:Exception do result := '('+IntToStr(integer(value))+')'; end; end; Usage: type TMyEnum = (e1=3, e2=-4, e3); TMyEnumStr=specialize Enumstr<TMyEnum>; var MyEnum: TMyEnum; MyString: string; begin MyEnum := e2; MyString := TMyEnumStr.tostr(MyEnum); // 'e2' MyEnum := TMyEnum(25); MyString := TMyEnumStr.tostr(MyEnum); // '(25)' -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/How-can-you-convert-an-enum-value-to-string-tp2821227p5721044.html Sent from the Free Pascal - General mailing list archive at Nabble.com. _______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal