Reputation: 1415
I am trying to work with an array of colors that is a constant. But Delphi keeps giving me compiling errors. I can't seem to figure out what I'm doing wrong...
const
Statuses : array[0..3] of TAlphaColors =
(
TAlphaColors.Lightgray, //error here: says insert a '(', even though I already have one
TAlphaColors.Yellow,
TAlphaColors.Limegreen,
TAlphaColors.Blue
);
Upvotes: 2
Views: 3227
Reputation: 612824
The problem you face is that TAlphaColor.Lightgray
, and indeed all the other TAlphaColor.XXX
that you name are ordinal true constant. Whereas TAlphaColors
is a record type.
Let's take a look at the relevant definitions:
type
TAlphaColor = type Cardinal;
TAlphaColorRec = record
const
Alpha = TAlphaColor($FF000000);
Aliceblue = Alpha or TAlphaColor($F0F8FF);
Antiquewhite = Alpha or TAlphaColor($FAEBD7);
.... many more color constant omitted
constructor Create(const Color: TAlphaColor);
class var ColorToRGB: function (Color: TAlphaColor): Longint;
case LongWord of
0:
(Color: TAlphaColor);
2:
(HiWord, LoWord: Word);
3:
{$IFDEF BIGENDIAN}
(A, R, G, B: System.Byte);
{$ELSE}
(B, G, R, A: System.Byte);
{$ENDIF}
end;
So the constants are not of type TAlphaColorRec
. Indeed it's one of the great frustrations of the language that you cannot declare nested constants in a record that are of that record type. These constants are ordinal true constants.
Note that the record itself has data in a variant part of the record. And the field of interest is the Color
field. So, you could declare your constant array like so:
const
Statuses : array[0..3] of TAlphaColors = (
(Color: TAlphaColors.Lightgray),
(Color: TAlphaColors.Yellow),
(Color: TAlphaColors.Limegreen),
(Color: TAlphaColors.Blue)
);
If only Embarcadero had had the foresight to allow us to write code like this:
type
TMyRecord = record
public
const
MyConst: TMyRecord = ();
end;
Upvotes: 3