Recent

Author Topic: Simple Array(open?) Question  (Read 5745 times)

MarsTeam

  • New Member
  • *
  • Posts: 11
Simple Array(open?) Question
« on: November 27, 2010, 04:36:17 pm »
Hello,

first let me say I tried googling and all, but I still haven't figured out solution to this problem.

Basically, what I want to do, is to have a class, which takes upon construction open array as a parameter so we can send it any arrays(like [0..3] [2..10] etc...).
But then, in class, I want to have a variable which would represent this array. So upon construction, a pointer to that open array is stored, and I can later do like for low(arr) to high(arr) - somewhere outside of contructor, without the need to always pass it as open array.

I've managed to do something like this, but while I understand why it doesn't work, I can't figure out how to do it right.

Code: [Select]
TTest = class
    public
      constructor Create(const vBitmap: array of TBitmap);
 
    private
    Array: array of TBitmap;
end;     

constructor TTest.Create(const vBitmap: array of TBitmap);
begin
  Array := vBitmap;
end;     

This returns:

Error: Incompatible types: got "Open Array Of TBitmap" expected "Dynamic Array Of TBitmap"

- I must have open array in constructor as parameter.
- I must be later able to work with this array which was passed into constructor, so I must store it somewhere, but only as a pointer.

Thanks for help.

Laksen

  • Hero Member
  • *****
  • Posts: 730
    • J-Software
Re: Simple Array(open?) Question
« Reply #1 on: November 27, 2010, 06:22:18 pm »
Try this

Code: [Select]
TBitmapArray = array of TBitmap; // Explicit dynamic array declaration

TTest = class
    public
      constructor Create(const vBitmap: TBitmapArray);
 
    private
    Array: TBitmapArray;
end;    

constructor TTest.Create(const vBitmap: TBitmapArray);
begin
  Array := vBitmap;
end;

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Simple Array(open?) Question
« Reply #2 on: November 27, 2010, 06:47:47 pm »
Code: [Select]
constructor Create(const vBitmap: array of TBitmap);Here, vBitmap is an open array.
Code: [Select]
private
    Array: array of TBitmap;
But here Array is dynamic array. Anyway, you shouldn't be able to use Array as attribute name since it's a reserved word.

I'm not sure about the lifetime of open arrays, so my idea is to make a copy of the passed open array instead (warning, untested code):
Code: [Select]
Array := Copy(vBitmap,Low(vBitmap),Length(vBitmap));

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Simple Array(open?) Question
« Reply #3 on: November 27, 2010, 09:25:12 pm »
Copy for arrays returns an error.

Tom83

  • Newbie
  • Posts: 6
Re: Simple Array(open?) Question
« Reply #4 on: November 28, 2010, 03:59:27 pm »
As written above, you can't use the word Array as an identifier, so first of all you have to change the name of the attribute Array, for example to Bitmaps:

Code: [Select]
private
  Bitmaps: array of TBitmap;

Then try to write the constructor like this:

Code: [Select]
constructor TTest.Create(const vBitmap: TBitmapArray);
var I: Integer;
begin
  SetLength(Bitmaps,Length(vBitmap));
  for I:=0 to Length(vBitmap) - 1 do
    Bitmaps[I] := vBitmap[I];
end;

 

TinyPortal © 2005-2018