What is the procedure to find an enumerated value from an integer? - PASCAL

What is the procedure to find an enumerated value from an integer?



- Pascal allows the conversion to take place of an integer to an enumerated type but it is not possible to do it other way around.

- The integer value can be found by using the function ord of the enumerated value type that can be used between the integers and enumerated.

- The space can be used with the speed penalty that can be used with the enumerated types and use of “unord” function to get the output.

type enum = (one, two, three, four, five, six, seven, eight, nine, ten);
var ei: enum;
etran: array [10] of enum;

begin

{ initalize translation array }
for ei := one to ten do etran[ord(ei)] := ei;
...
ei := etran[5];

- The translation of the integer to the enumeration type is difficult task and the use of array lookup is being used. The translation array depends on the size of the enumerated type or enum.
Post your comment