Recent

Author Topic: [SOLVED] Creating a three dimensional array constant  (Read 323 times)

fatmonk

  • Full Member
  • ***
  • Posts: 232
[SOLVED] Creating a three dimensional array constant
« on: December 11, 2019, 06:37:57 pm »
I'm struggling with a bit of syntax here...

I want to create a constant array to hold a set of colour schemes.

To this end I have defined a record type 'colourScheme' as follows:

Code: [Select]
  { colourScheme }
  TcolourScheme = record
    fg: Tcolor;
    bg: Tcolor;
  end;

I then want to define a constant array of these, but the following does not compile:

Code: [Select]
const
  colourSchemes: array of TcolourScheme = [(fg: clBlack; bg: clWhite),
                                           (fg: clGreen, bg: clBlack)];

I want to be able to access the colour schemes with something like:

Code: [Select]
myLabel.Font.Color:=colourSchemes[0].fg;
myLabel.Color:=colorSchemes[0].bg;

Is this possible? I am anywhere near the correct syntax?

-FM
« Last Edit: December 12, 2019, 04:34:16 pm by fatmonk »

wp

  • Hero Member
  • *****
  • Posts: 6664
Re: Creating a three dimensional array constant
« Reply #1 on: December 11, 2019, 06:41:37 pm »
Of course, this is possible. You only must specify the array limits in the declaration, and use round outer parentheses:
Code: Pascal  [Select]
  1. const
  2.   colourSchemes: array[0..1] of TcolourScheme = (
  3.     (fg: clBlack; bg: clWhite),
  4.     (fg: clGreen, bg: clBlack)
  5.   );
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

PascalDragon

  • Hero Member
  • *****
  • Posts: 905
  • Compiler Developer
Re: Creating a three dimensional array constant
« Reply #2 on: December 12, 2019, 09:50:48 am »
In the upcoming FPC 3.2 the following will work as well:

Code: Pascal  [Select]
  1. const
  2.   colourSchemes: array of TcolourScheme = (
  3.     (fg: clBlack; bg: clWhite),
  4.     (fg: clGreen, bg: clBlack)
  5.   );

fatmonk

  • Full Member
  • ***
  • Posts: 232
Re: Creating a three dimensional array constant
« Reply #3 on: December 12, 2019, 04:34:03 pm »
@wp: Thanks, all good now - I was so close...

@PascalDragon: Even better.. it does seem a bit daft having to manually count how many elements I have (and go back to change the size specification if I add more) when they are there to be seen. I can understand specifying the size if you are not populating there and then, but it seems clunky otherwise. This is a nice improvement.

-FM

PaulRowntree

  • Full Member
  • ***
  • Posts: 109
    • Paul Rowntree
Re: Creating a three dimensional array constant
« Reply #4 on: December 12, 2019, 05:55:02 pm »
In the upcoming FPC 3.2 the following will work as well:

Code: Pascal  [Select]
  1. const
  2.   colourSchemes: array of TcolourScheme = (
  3.     (fg: clBlack; bg: clWhite),
  4.     (fg: clGreen, bg: clBlack)
  5.   );
How far out would we know (or the roadmap updated to show) when FPC 3.2 will be packaged into laz?
Paul Rowntree
- coding for instrument control, data acquisition & analysis, CNC systems

PascalDragon

  • Hero Member
  • *****
  • Posts: 905
  • Compiler Developer
Re: Creating a three dimensional array constant
« Reply #5 on: December 13, 2019, 09:48:19 am »
@PascalDragon: Even better.. it does seem a bit daft having to manually count how many elements I have (and go back to change the size specification if I add more) when they are there to be seen. I can understand specifying the size if you are not populating there and then, but it seems clunky otherwise. This is a nice improvement.
Well, there is a difference. The syntax that wp showed is a static array. While the new one is a dynamic array. You can't resize it, cause it's constant, but it might make a difference depending on the use case.

How far out would we know (or the roadmap updated to show) when FPC 3.2 will be packaged into laz?
FPC 3.2 needs to be released first (we had hoped for before Christmas, but that got busted, so probably some time early '20) before it can be packaged into a Lazarus release.