### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: A constant array of records - Question  (Read 510 times)

#### neutrino70

• Newbie
• Posts: 4
##### A constant array of records - Question
« on: February 01, 2023, 07:51:54 pm »
hi,
I've got a problem:

This is, what I declared:

type
deckEnum = (ENG, FRA, GER, DIA);

deckType = record
PropSt: String;
Fields: Byte;
Stones: Byte;
FreeFd: Byte;
Moves:  Byte;
Diags:  Byte;
end;

const
decks : array [deckEnum] of deckType = (
(PropSt: 'ENG'; Fields: 33; Stones: 32; FreeFd: 16; Moves:  76; Diags: 60;),
(PropSt: 'FRA'; Fields: 37; Stones: 36; FreeFd: 18; Moves:  92; Diags: 76;),
(PropSt: 'GER'; Fields: 45; Stones: 44; FreeFd: 22; Moves: 108; Diags: 76;),
(PropSt: 'DIA'; Fields: 41; Stones: 40; FreeFd: 20; Moves: 100; Diags: 92;)
);

Question: How can I access the Stones-item in decks[DIA] (the 40 in the last line of decks)?

#### Paolo

• Sr. Member
• Posts: 367
##### Re: A constant array of records - Question
« Reply #1 on: February 01, 2023, 08:23:21 pm »

Code: Pascal  [Select][+][-]
1. Deck[dia].stones
2.
Is enough

#### neutrino70

• Newbie
• Posts: 4
##### Re: A constant array of records - Question
« Reply #2 on: February 02, 2023, 06:04:16 am »
Thanks you.
That was, what I thought.

But Deck[dia].stones should read decks[DIA].Stones, isn't it?

But when I write :

decks[DIA].Stones, I get Error: Illegal expression

#### KodeZwerg

• Hero Member
• Posts: 1189
##### Re: A constant array of records - Question
« Reply #3 on: February 02, 2023, 06:13:06 am »
But when I write :

decks[DIA].Stones, I get Error: Illegal expression
Can you tell how you write?
Can you enclose your codes in tags by clicking on the fancy [ # ] button?

Code: Pascal  [Select][+][-]
1. program Project1;
2. {\$IFDEF MSWINDOWS}{\$APPTYPE CONSOLE}{\$ENDIF}
3.
4. type
5.   TDeckEnum = (ENG, FRA, GER, DIA);
6.
7.   TDeckType = record
8.     PropSt: String;
9.     Fields: Byte;
10.     Stones: Byte;
11.     FreeFd: Byte;
12.     Moves:  Byte;
13.     Diags:  Byte;
14.   end;
15.
16. const
17.   CDecks : array [TDeckEnum] of TdeckType = (
18.     (PropSt: 'ENG'; Fields: 33; Stones: 32; FreeFd: 16; Moves:  76; Diags: 60;),
19.     (PropSt: 'FRA'; Fields: 37; Stones: 36; FreeFd: 18; Moves:  92; Diags: 76;),
20.     (PropSt: 'GER'; Fields: 45; Stones: 44; FreeFd: 22; Moves: 108; Diags: 76;),
21.     (PropSt: 'DIA'; Fields: 41; Stones: 40; FreeFd: 20; Moves: 100; Diags: 92;)
22.   );
23.
24. begin
25.   WriteLn('Stoned: ', CDecks[DIA].Stones);
27. end.
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

#### TRon

• Hero Member
• Posts: 834
##### Re: A constant array of records - Question
« Reply #4 on: February 02, 2023, 06:13:50 am »
decks[DIA].Stones, I get Error: Illegal expression

That statement alone returns the value 40.

And that is all it does. it does not store it into another variable it does not feed the value to another function or otherwise. So you basically tell the compiler "40" and ask it to have fun with it

To illustrate the statement you used does exactly the same as::
Code: Pascal  [Select][+][-]
1. begin
2.   40;
3. end.
4.

Which does not make sense and so the compiler tells you.

After all, if it should work that way then only for the value 42  (the answer to anything in the universe)

#### neutrino70

• Newbie
• Posts: 4
##### Re: A constant array of records - Question
« Reply #5 on: February 02, 2023, 06:44:07 am »
you're both right.
but actually, I do an assignment in the const-department:

const
Nr_dMoves = 76;
Nr_dStones = decks[DIA].Stones;

this results in "Error: Illegal expression"

Later on, I want to use the expression in a declaration such as
dMoves : array[0 .. decks[DIA].Stones -1] of CertainType = (... etc);

and there I get "Can't evaluate constant expression"

#### TRon

• Hero Member
• Posts: 834
##### Re: A constant array of records - Question
« Reply #6 on: February 02, 2023, 06:58:17 am »
you're both right.
but actually, I do an assignment in the const-department:

const
Nr_dMoves = 76;
Nr_dStones = decks[DIA].Stones;

this results in "Error: Illegal expression"
Ah, sorry for the confusion. The reason for the error in that case is that your constant is not actually a real const anymore. The moment you type(d) your const definition it is not a real constant anymore but is treated like a variable would be treated. All other variations will result in similar compiler messages.

• Hero Member
• Posts: 12933
##### Re: A constant array of records - Question
« Reply #7 on: February 02, 2023, 07:31:01 am »
For your code to work and allow modifications Assignable Typed Consts must be ON. ( {\$J+} )
Code: Pascal  [Select][+][-]
1. {\$IFDEF MSWINDOWS}{\$APPTYPE CONSOLE}{\$ENDIF}
2. {\$J+}
3.
4. type
5.   TDeckEnum = (ENG, FRA, GER, DIA);
6.
7.   TDeckType = record
8.     PropSt: String;
9.     Fields: Byte;
10.     Stones: Byte;
11.     FreeFd: Byte;
12.     Moves:  Byte;
13.     Diags:  Byte;
14.   end;
15.
16. const
17.   CDecks : array [TDeckEnum] of TdeckType = (
18.     (PropSt: 'ENG'; Fields: 33; Stones: 32; FreeFd: 16; Moves:  76; Diags: 60;),
19.     (PropSt: 'FRA'; Fields: 37; Stones: 36; FreeFd: 18; Moves:  92; Diags: 76;),
20.     (PropSt: 'GER'; Fields: 45; Stones: 44; FreeFd: 22; Moves: 108; Diags: 76;),
21.     (PropSt: 'DIA'; Fields: 41; Stones: 40; FreeFd: 20; Moves: 100; Diags: 92;)
22.   );
23.
24. begin
25.   CDecks[DIA].Stones := 100;  // const is writable because of \$J+
26.   WriteLn('Stoned: ', CDecks[DIA].Stones);
28. end.

If this is not desirable for all code you can also do that locally like this:
Code: Pascal  [Select][+][-]
1. const
2. {\$push}{\$J+} // just this const is writable
3.   CDecks : array [TDeckEnum] of TdeckType = (
4.     (PropSt: 'ENG'; Fields: 33; Stones: 32; FreeFd: 16; Moves:  76; Diags: 60;),
5.     (PropSt: 'FRA'; Fields: 37; Stones: 36; FreeFd: 18; Moves:  92; Diags: 76;),
6.     (PropSt: 'GER'; Fields: 45; Stones: 44; FreeFd: 22; Moves: 108; Diags: 76;),
7.     (PropSt: 'DIA'; Fields: 41; Stones: 40; FreeFd: 20; Moves: 100; Diags: 92;)
8.   );
9. {\$pop}
« Last Edit: February 02, 2023, 07:35:57 am by Thaddy »
In memory of Gordon Moore  (January 3, 1929 – March 24, 2023) Just double the heaven every two years from now.

#### neutrino70

• Newbie
• Posts: 4
##### Re: A constant array of records - Question
« Reply #8 on: February 02, 2023, 07:55:56 am »
Thank you so much