Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!unmvax!nmtsun!jrwsnsr From: jrwsnsr@nmtsun.nmt.edu (Jonathan R. Watts) Newsgroups: comp.lang.pascal Subject: Re: Another various question about TP 5.0 Message-ID: <3579@nmtsun.nmt.edu> Date: 1 Dec 89 02:53:41 GMT References: <303@usna.MIL> Organization: New Mexico Tech, Socorro NM Lines: 47 In article <303@usna.MIL>, baldwin@usna.MIL (J.D. Baldwin) writes: > Is there a good, clean way to convert the value of an enumerated ordinal > type to a string value? > > That is, what does the function ConverterFunction(somevalue:sometype) : string > look like in this fragment: > > type veggies = (carrots, limabeans, beets, toadstools); . . . > var whatveg : veggies; . . . > > whatveg := carrots; > . . . > writeln(ConverterFunction(whatveg)); > > so that writeln statement will output the string "carrots"? This question > is not exactly mission-critical to me, but an answer would be nice. If > replies are by e-mail, I'll summarize. Unfortunately, there's no easy way to do this, as the names of the values are not stored in the output file! However, in Turbo Pascal, you could do the following: type string10 = string[10]; veggies = (carrots, limabeans, beets, toadstools); const veggies_string : array[0..3] of string10 = ('carrots', 'limabeans', 'beets', 'toadstools'); var whatveg : veggies; function ConverterFunction(ToConvert : veggies) : string10; begin ConverterFunction := veggies_string[Ord(ToConvert)]; end; begin whatveg := carrots; writeln(ConverterFunction(whatveg)); end. Unfortunately, you can only use typed constants (the const above) in Turbo Pascal (as far as I know). With standard pascal, you would have to make the array a standard var, and initialize it in the program body. - Jonathan Watts jrwsnsr@jupiter.nmt.edu