Board index » delphi » array property with open array indexing

array property with open array indexing

Hi.  I have used array properties fine with longint indexes:

xx = class
  function getyy (ndex : longint) : char;
  property yy [ndex : longint] : char read getyy;
end;

The book says that ANY type of index is permitted.  But open array
parameters are flagged as 'incompatible type'.  Is this a language
limitation or a bug:

xx = class
  function getyy (NDEX : ARRAY OF LONGINT) : char;
  property yy [NDEX : ARRAY OF LONGINT] : char read getyy;
end;

Any thoughts?  TIA.
Barry Laxton

 

Re:array property with open array indexing


Quote
A Laxton wrote:

> Hi.  I have used array properties fine with longint indexes:

> xx = class
>   function getyy (ndex : longint) : char;
>   property yy [ndex : longint] : char read getyy;
> end;

> The book says that ANY type of index is permitted.  But open array
> parameters are flagged as 'incompatible type'.  Is this a language
> limitation or a bug:

> xx = class
>   function getyy (NDEX : ARRAY OF LONGINT) : char;
>   property yy [NDEX : ARRAY OF LONGINT] : char read getyy;
> end;

        The deal here is that any actual variable type can be an
index to an array property. But "array of longint" is not a type,
it's actually closer to being a compiler directive. Hmm, didn't
put that too well...

Take the example in the docs and change it like so:

type

  huh = array[0..10] of longint;

type
  TDemoComponent = class(TComponent)
  private
    function GetNumberName(Index: huh): string;
  public
    property NumberName[Index: huh]: string read GetNumberName;
  end;

        That works fine. And in fact as far as I can see I can define
huh to be any type at all, and then using huh as the index for an array
property works. But "array of longint" is not a type: If you say

type
        huh = array of longint;

you get an error _there_.

        If you really want to use 'array of longint' as an index you could fake
it more or less like so:

 type
        BigArray = array[0..1000] of longint;
        PBigArray = ^BigArray;

        huh = record
                Data: PBigArray;
                Size: integer;
        end;

type
  TDemoComponent = class(TComponent)
  private
    function GetNumberName(Index: huh): string;
  public
    property NumberName[Index: huh]: string read GetNumberName;
  end;

        Now of course if you're going to use one of these huh's
you need to allocate memory for the elements of Data^ that
you're going to use, then you would fill in the right value for
Size, and finally write your methods to look at Size to make
certain they don't try to access unallocated memory...

--
David Ullrich

?his ?s ?avid ?llrich's ?ig ?ile
(Someone undeleted it for me...)

Re:array property with open array indexing


Quote
David Ullrich <ullr...@math.okstate.edu> wrote:
>    The deal here is that any actual variable type can be an
>index to an array property. But "array of longint" is not a type,
>it's actually closer to being a compiler directive. Hmm, didn't
>put that too well...
>Take the example in the docs and change it like so:
>type

>  huh = array[0..10] of longint;
>type
>  TDemoComponent = class(TComponent)
>  private
>    function GetNumberName(Index: huh): string;
>  public
>    property NumberName[Index: huh]: string read GetNumberName;
>  end;
>    That works fine. And in fact as far as I can see I can define
>huh to be any type at all, and then using huh as the index for an array
>property works. But "array of longint" is not a type: If you say
>type
>    huh = array of longint;
>you get an error _there_.
>    If you really want to use 'array of longint' as an index you could fake
>it more or less like so:
> type
>    BigArray = array[0..1000] of longint;
>    PBigArray = ^BigArray;
>    huh = record
>            Data: PBigArray;
>            Size: integer;
>    end;
>type
>  TDemoComponent = class(TComponent)
>  private
>    function GetNumberName(Index: huh): string;
>  public
>    property NumberName[Index: huh]: string read GetNumberName;
>  end;
>    Now of course if you're going to use one of these huh's
>you need to allocate memory for the elements of Data^ that
>you're going to use, then you would fill in the right value for
>Size, and finally write your methods to look at Size to make
>certain they don't try to access unallocated memory...
>--
>David Ullrich
>?his ?s ?avid ?llrich's ?ig ?ile
>(Someone undeleted it for me...)

Thank you.

Rats.
And it looked so pretty, too.

barry

Other Threads