program testnums;
//{$APPTYPE CONSOLE}
uses
Classes, SysUtils;
type
stType = (stLite := -100, stDb, stBothLite, stBothDb); //settings
location Type
stTypes = Array [stType] of integer;
var
d : stType;
s : string;
i : integer;
begin
writeln('');
for i := Ord(low(stType)) to Ord(high(stType)) do
begin
d := stTypes[i - Ord(low(stType))];
writestr(s,d);
writeln(d + ' ' + IntToStr(Integer(i)));
end;
readln;
end.Mailing lists / fpc-pascal / 2010-09
What is wrong with this enum and array related code
6 messages · 3 people ·
- FFrank Church 3
- VVincent Snijders 2
- HHonza 1
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
I need to elaborate more on this code
On this line
d := stTypes[i - Ord(low(stType))];since stTypes is an array, I expect stTypes[n], with n being 1 to 4, to retrieve an array value and as 'i - Ord(low(stType)) evaluates to integer it should compile but it generates an error.
What would be the correct way?
Frank Church wrote 31 lines
program testnums; //{$APPTYPE CONSOLE} uses Classes, SysUtils; type stType = (stLite :=…
2010/9/12 Frank Church :
Show quoted text 24 lines
I think the array and enum are correct, but the usage isn't. I did not compile this code, but I expect a type mismatch error. You are using an integer as index in d := stTypes[i - Ord(low(stType))], but you declared stTypes an array to be indexed by stType.
I need to elaborate more on this code On this line d := stTypes[i - Ord(low(stType))];…
2010/9/12 Frank Church :
Show quoted text 31 lines
for d:= low(stType) to high(stType) do> begin > d := stTypes[i - Ord(low(stType))];
i := stTypes[d];Show quoted text 25 lines
Vincent Snijders wrote 54 lines
Does that mean that although an enum is an ordinal type, an integer cannot be coerced into using it to index an array?
Show quoted text 14 lines
2010/9/12 Frank Church :
Does that mean that although an enum is an ordinal type, an integer cannot be coerced into using it to index an array?
IMO just cast it to the correct type (of the array index) giving the compiler a chance to accept it.
