Jump to content
Nigel Thomas

D2007: Initialise byte array in const record

Recommended Posts

Using Delphi D2007.

 

I'm trying to do the following:

 

type
  FileSig = record
    Offset: Integer;
    arrSig: array of byte;
  end;

const
  sig1: FileSig =
    (Offset: 10;
     arrSig: array [0..2] of byte = ($00,$01,$02);
    )

But I can't: E2029 ')' expected but ',' found

 

In D10+ I can do:

const
  sig1: FileSig =
    (Offset: 10;
     arrSig: [$00,$01,$02];
    )

Is there a way I can do similar in D2007?

Share this post


Link to post
27 minutes ago, Nigel Thomas said:

But I can't: E2029 ')' expected but ',' found

What do you get when you try

const
  sig1: FileSig =
    (Offset: 10;
     arrSig: [$00,$01,$02];
    )

in D2007?

Share this post


Link to post
3 minutes ago, JonRobertson said:

What do you get when you try


const
  sig1: FileSig =
    (Offset: 10;
     arrSig: [$00,$01,$02];
    )

in D2007?

E2010 incompatible types: 'dynamic array' and 'Set'

Share this post


Link to post
1 hour ago, Lars Fosdal said:

Replace the brackets with parenthesis?

^

 

I don't have D2007.  Your D10+ version works in freepascal using {$mode delphi}

 

and this works in fpc without it :

 

  sig2: FileSig =
    (Offset: 10;
     arrSig: ($00,$01,$02);
    );
  • Like 1

Share this post


Link to post
Posted (edited)

This works for me in XE7. 

 

type
  FileSig = record
    Offset: Integer;
    text: string;
    arrSig: array of byte;
    arrStr: array of char;
  end;

const
  sig1: FileSig = ( offset:10;  text:'test';  arrsig:[65,66,67]; arrStr:['a','b','c'] );

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  memo1.Lines.Add(sig1.Offset.ToString());
  memo1.Lines.Add(sig1.text);
  memo1.Lines.Add( inttostr(sig1.arrSig[0]) );
  memo1.Lines.Add('[' + ansistring(sig1.arrSig) + ']');
  memo1.Lines.Add('[' + string(sig1.arrStr) +']');
end;

 

Output Results: 

 

10
test
65
[ABC]
[abc]


 

 

Edited by JohnLM
removed pstring()

Share this post


Link to post
8 hours ago, Nigel Thomas said:

Using Delphi D2007.

 

I'm trying to do the following:

 


type
  FileSig = record
    Offset: Integer;
    arrSig: array of byte;
  end;

const
  sig1: FileSig =
    (Offset: 10;
     arrSig: array [0..2] of byte = ($00,$01,$02);
    )

But I can't: E2029 ')' expected but ',' found

 

In D10+ I can do:


const
  sig1: FileSig =
    (Offset: 10;
     arrSig: [$00,$01,$02];
    )

Is there a way I can do similar in D2007?

No, this is not possible in Delphi 2007. You can declare typed constants for fixed length arrays, but not dynamic arrays.

  • Thanks 1

Share this post


Link to post
5 hours ago, David Heffernan said:

No, this is not possible in Delphi 2007. You can declare typed constants for fixed length arrays, but not dynamic arrays.

Thanks. I've worked around it by declaring FileSig.arrSig as a fixed array, e.g: array [0..4] of byte, then I can set the const array using padding where necessary e.g. arrSig: ($00, $01, $02, $00, $00); By adding a SigLength to the FileSig type I will know what padded bytes I can remove when I come to using the array.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×