• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1693
  • Last Modified:

CopyMemory() with TObject

Hi,

I have a problem copying data from an array to a TObject.  The following is an extract of the code.


type THeader = class(TObject)
  Data: array[1..18] of byte;
end;
..
..

procedure TForm1.ButtonClick(Sender: TObject);
var
  Head: array[1..18] of byte;
  i: integer;
  Header: THeader;
  str1: string;
  str2: string;
begin
  Header := THeader.Create;
 
  for i:=1 to 18 do
    Head[i] := i+1;
 
 //** Problem with this??
  CopyMemory(Header, @Head, SizeOf(Head));

  for i:=1 to 18 do
    begin
      Str1 := Str1 + ' ' + intToStr(Header.Data[i]);
      Str2 := Str2 + ' ' + IntToStr(Head[i]);
    end;

    Showmessage(str1 + ' : ' + str2);
end;

The data in Head is:2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

But when I copy it to the Header Object, the data is: 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 0 0 0.

How can I fix this so Header is the same as the Head?

TIA.
0
engdmorr
Asked:
engdmorr
1 Solution
 
geobulCommented:
Hi,

CopyMemory(@(Header.Data), @Head, SizeOf(Head));

Regards, Geo
0
 
engdmorrAuthor Commented:
Works a treat, thanks!
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now