Re: checking arbitrary bounds
- From: "Tom de Neef" <tdeneef@xxxxxxxx>
- Date: Sat, 28 May 2005 00:42:38 +0200
"Pawel Slusarz" <wiedzmin@xxxxxxxxx> schreef in bericht
news:iQLle.11169$PS3.2823@xxxxxxxxxxxx
> Surely someone must've run into this before. I want to write a
> function that combines the functionality of Low() and High().
>
> // non-working example
> function IsIndexWithinBounds(i : integer; var a) : boolean;
> begin
> Result := (i >= low(a)) and (i <= high(a));
> end;
>
> I'd settle for arbitrary arrays, if the above can't be done.
>
> Thanks for your comments,
> Paul
>
>
The difficulty is with arrays with a non-zero lower bound.
If you use the following type and function:
type
Tar5 = array[5..15] of integer;
function withinBounds(k : integer; var a : array of integer) : boolean;
begin
result:=(k>=low(a)) AND (k<=high(a))
end;
then the result will not be correct when you pass it a variable of type
Tar5. That is because low() will always return zero for an open array
variable. The space of the variable is mapped onto a zero-based index.
But if you define the function specifically for Tar5, like
function withinBounds(k : integer; var a : Tar5) : boolean;
begin
result:=(k>=low(a)) AND (k<=high(a))
end;
then it will return the correct result when called with a Tar5 parameter.
If you have a limited number of array types for which you want to test the
index, then define functions like the above with the overload directive. So,
you make one for open integer arrays, one for open extended arrays, several
for predefined arrays, etc.
Tom
.
- References:
- checking arbitrary bounds
- From: Pawel Slusarz
- checking arbitrary bounds
- Prev by Date: Re: How to open a windows folder ?
- Next by Date: Re: Delphi 7 - convert a bmp 32bit into a 24bit
- Previous by thread: Re: checking arbitrary bounds
- Next by thread: Re: checking arbitrary bounds
- Index(es):
Relevant Pages
|
|