Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!wuarchive!udel!rochester!rit!ritcsh!mattl From: mattl@ritcsh.cs.rit.edu (FaceMan) Newsgroups: comp.lang.pascal Subject: Re: Another various question about TP 5.0 Summary: Getting values of a type Message-ID: <471@ritcsh.cs.rit.edu> Date: 1 Dec 89 15:19:31 GMT References: <303@usna.MIL> Organization: Computer Science House @ RIT, Rochester, NY Lines: 41 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: [ text deleted ] > writeln(ConverterFunction(whatveg)); > > so that the writeln statement will output the string "carrots"? This question One way you could do it is to simply write a function with a CASE statement that assigns the correct value to the function and returns. However, there is an (I think, anyways) easier and more efficient way to do this. What you do is use typed constants to define your values like this: TYPE Days = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday); DayStr = String[4]; CONST Day : ARRAY[Days] OF DayStr = ('Sun','Mon','Tues','Wed','Thurs','Fri','Sat'); - or - Day : ARRAY[Sunday..Saturday] < Rest of line looks identical > Now, if you want the string value of a day, simply use: WriteLn(Day[Sunday]); -> Would write out "Sun".... Of course, you would have to write one for each type you define, but it is probably the only way. Once the program is compiled, it really knows nothing about the names the types uses, only their ordinal values. One small sidenote: These constants can be used as pre-defined variables. In other words, they start out with their initial values, but you could, for example, change the "Wed" to "Mid" in the middle of your program. -- You can lead a horse to water. | Matthew | mattl@ritcsh.cs.rit.edu But if you can get him to | "FaceMan" | mal6315@ultb.isc.rit.edu lie down on his back and float, | Lecher | mal6315@ritvax you've really got something there! |=================|=========================