How to iterate initialized enumerated types with Delphi 6 and avoid the "out of bounds" error? -
i using delphi 6 professional. interfacing dll libraty declares enumberated type follows:
textdllenum = (enum1 = $0, enum2 = $1, enum3 = $2, enum4 = $4, enum5 = $8, enum6 = $10);
as can see initialized values not contiguous. if try iterate type using loop follows:
var e: textdllenum; begin e := low(texttodllenum) high(texttodllenum) ... // more code end;
delphi still increments e 1 each loop invocation , thereby creates numeric values e not members of enumerated type (for example, '3'), , resulting in 'out of bounds' error. how can iterate enumerated type in loop generates valid values enumerated type?
thanks.
by defining set of constants...
type textdllenum = (enum1 = $0, enum2 = $1, enum3 = $2, enum4 = $4, enum5 = $8, enum6 = $10); const cextdllenumset = [enum1, enum2, enum3, enum4, enum5, enum6]; var e: textdllenum; begin e := low(textdllenum); while e <= high(textdllenum) begin if e in cextdllenumset writeln(ord(e)); inc(e); end; readln; end.
and implemented iterator - fun...
type textdllenum = (enum1 = $0, enum2 = $1, enum3 = $2, enum4 = $4, enum5 = $8, enum6 = $10); const cextdllenumset = [enum1, enum2, enum3, enum4, enum5, enum6]; type tmyiterator = class private fvalue: textdllenum; public constructor create; function next: textdllenum; function hasnext: boolean; end; constructor tmyiterator.create; begin fvalue := low(textdllenum); end; function tmyiterator.hasnext: boolean; begin result := fvalue <= high(textdllenum); end; function tmyiterator.next: textdllenum; begin result := fvalue; repeat inc(fvalue); until (fvalue in cextdllenumset) or (fvalue > high(textdllenum)) end; var myiterator: tmyiterator; begin myiterator := tmyiterator.create; while myiterator.hasnext writeln(ord(myiterator.next)); myiterator.free; readln; end.
Comments
Post a Comment