Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-09

What is wrong with this enum and array related code

6 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Frank Church
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.
Frank Church ======================= http://devblog.brahmancreations.com
Frank Church
to Frank Church

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 := -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. -- Frank Church ======================= http://devblog.brahmancreations.com
Frank Church ======================= http://devblog.brahmancreations.com
Vincent Snijders
to Frank Church: program testnums; //{$APPTYPE CONSOLE} uses Classes, SysUtils; type stType = (stLite :=…

2010/9/12 Frank Church :

Show quoted text 24 lines
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.

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.

Vincent Snijders
to Frank Church: 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
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? On 12 September 2010 12:51, Frank Church wrote: > > 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
for  d:= low(stType) to high(stType) do
>  begin >   d := stTypes[i - Ord(low(stType))];
i := stTypes[d];
Show quoted text 25 lines
>    writestr(s,d); >    writeln(d + ' ' + IntToStr(Integer(i))); >  end; >  readln; > > end. > > -- > Frank Church > > ======================= > http://devblog.brahmancreations.com -- Frank Church ======================= http://devblog.brahmancreations.com _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Frank Church
to Vincent Snijders
Vincent Snijders wrote 54 lines
2010/9/12 Frank Church : > 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? > > On 12 September 2010 12:51, Frank Church wrote: >> >> 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 for d:= low(stType) to high(stType) do >> begin >> d := stTypes[i - Ord(low(stType))]; i := stTypes[d]; >> writestr(s,d); >> writeln(d + ' ' + IntToStr(Integer(i))); >> end; >> readln; >> >> end. >> >> -- >> Frank Church >> >> ======================= >> http://devblog.brahmancreations.com > > >

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
> -- > Frank Church > > ======================= > http://devblog.brahmancreations.com > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Frank Church ======================= http://devblog.brahmancreations.com
Honza
to Frank Church

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.

bflm freepascal-bits.blogspot.com