Link to home
Start Free TrialLog in
Avatar of wildzero
wildzero

asked on

Enumerated types and sets of - counts and loops

:-)
Lets say I have this.

type
  TThing = (tgsOne, tgsTwo, tgsThree);
  TThings = set of TThing;

and then I do

var
  Things : TThings;
begin
  include(Things,tgsOne);

and that is all good, Things now contains tgsOne in the set.
then I do this
include(Things,tgsTwo);

now I want to know how many items are in the set.
showmessage( IntToStr(length(Things)) );
that dosn't want to work.

Also how can I loop the items in my set to see if they are in there/do stuff. Do I have to do
var
  Things : TThings;
  Thing : TThing;
begin
  include(Things,tgsOne);

  For Thing := low(TThing) to high(Thing) do
    If Thing in Things then
     //-- do something

:-)



ASKER CERTIFIED SOLUTION
Avatar of mokule
mokule
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wildzero
wildzero

ASKER

What about getting a count of the number of items in the set?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
P.S.  the pseudo code I posted has the added advantage that it does not matter what type of set you pass it.  The down side is that it tests for values that cannot exist within a specific set (it is testing for all 32 possibilities - if your set only contains 3...).  It should be possible to create a function where you pass in the type of set and the value and it counts it by looping the appropriate number of times for the set.  I just don't have time to try it tonight, sorry.
Avatar of Lukasz Zielinski
just wondering what if you do this:
Include(Things, tgsNothing);
will Things contain any elements or will it contain nothing? ;)


sorry couldn't resist:)

ziolko.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forced accept.

Computer101
EE Admin