Path: utzoo!utgpu!watmath!watdcsu!dmurdoch From: dmurdoch@watdcsu.waterloo.edu (D.J. Murdoch - Statistics) Newsgroups: comp.lang.pascal Subject: Re: Enumerated types in TP40 Keywords: none Message-ID: <5390@watdcsu.waterloo.edu> Date: 4 Jan 89 14:39:50 GMT References: <363@lafcol.UUCP> Reply-To: dmurdoch@watdcsu.waterloo.edu (D.J. Murdoch - Statistics) Organization: U. of Waterloo, Ontario Lines: 31 In article <363@lafcol.UUCP> pilgrimk@lafcol.UUCP (Guru Jee) writes: >How can the number of elements in an enumerated type be determined? > > e.g. type > Enum1 = (One, Two, Three, Four); > Enum2 = (_One, _Two, _Three); > >I would like to get a value of 4 for the no. of elements in any >variable declared as "Enum1" and 3 for those declared as "Enum2". Is >this possible? Not easily - TP doesn't give any min and max functions that work on variables, let alone types. However, if you are really desperate, it can be done, sort of. If you enable range checking, then assigning a byte with value 4 to a variable of type Enum1 will cause an error. So, one way to count an enumerated type is to see how far you get in assigning values starting with 0 to it. There's a faster but even worse way to do it, however. The way range checking works in TP4 (I'm not sure about any other version) is to store the upper and lower limits as longints somewhere in the code segment, and pass the address of these limits, along with the value to be checked, to a little subroutine in the system unit. I once wrote an inline routine that could look back through the code until it found the pointer to the limits, and returned that. Then you could find out (and even change) the limits that the range checker was using. I was applying this to checking the limits on the subscripts of an array, but I imagine the same idea would work to find the limits on any type. After I wrote it, I couldn't find any situation where I was desperate enough to want to use it, and I've lost it now. Duncan Murdoch