>Given an Object Pascal set, is there a way to find the number of elements?
I'm not a Pascal expert, so there may be an easier way. My approach
would be
( Ord( High( MySet )) - Ord( Low( MySet)) ) + 1
High returns the last element in the set, Low returns the first
element in the set, and Ord returns the ordinal position of an element
in the set.
Chuck Gadd
Director of Software Development, Cyber FX Communications.
e-mail:cgadd-...@cyber-fx.com http://www.csd.net/~cgadd
Remove the -NOSPAM from my email address to send me e-mail.
*** I boycott businesses that send me unsolicited email advertisements ***
>On Mon, 28 Jul 1997 22:56:14 -0800, j...@playfair.stanford.edu
>(Jonathan Buckheit) wrote:
>>Given an Object Pascal set, is there a way to find the number of elements?
>I'm not a Pascal expert, so there may be an easier way. My approach
>would be
> ( Ord( High( MySet )) - Ord( Low( MySet)) ) + 1
>High returns the last element in the set, Low returns the first
>element in the set, and Ord returns the ordinal position of an element
>in the set.
That would return the maximum number of elements that could be
assigned to set.
> On Mon, 28 Jul 1997 22:56:14 -0800, j...@playfair.stanford.edu
> (Jonathan Buckheit) wrote:
>
> >Given an Object Pascal set, is there a way to find the number of elements?
>
> I'm not a Pascal expert, so there may be an easier way. My approach
> would be
>
> ( Ord( High( MySet )) - Ord( Low( MySet)) ) + 1
>
> High returns the last element in the set, Low returns the first
> element in the set, and Ord returns the ordinal position of an element
> in the set.
Chuck -- can High and Low operate on a set? I thought just on arrays.
Well, actually I was thinking of Ordinal types rather than sets (I
told you I'm not a Pascal guru!)
-------------------------------------------------------
Ok, I'm not done yet! Maybe someone who knows more about Sets can
help me out, but I'll dig a little deeper first:
If you have an ordinal type
type MyType = (AAA,BBB,CCC,DDD,EEE,FFF,GGG);
then you declare your set variable:
var TestSet : set of MyType;
and declare a counter variable and a variable for a loop:
var iCount : Integer;
var Loop : MyType;
then add elements to your set
TestSet := [BBB,EEE,GGG];
then count them up this way:
iCount := 0;
ForLoop := low(Loop) to high(Loop) do
if Loop in TestSet then
Inc( iCount );
In this case, iCount equals 3, so it works!
Now, can any PASCAL guru out there clean this mess up, and maybe
encapulate it into a function call?