High() and Low() Uh-Oh!

Started by
14 comments, last by Xorcist 22 years, 5 months ago
But, in my case this code won''t work:

  procedure ClearArray(out SomeArray: array of integer);beginend;  


because using array of integer expects a singly dimensioned array, and won''t accept arrays with two dimensions or more (I''ve tried).

I see what you mean about the number not mattering, if they''re variables. That actually helps out a hell of a lot because now I don''t need that mess of code I wrote before to abstract the numbers, I can just use a default variable to get my results as such:

  Procedure Clear(var tmpArray: TArray);Var  X,Y: Integer;     Tmp: Integer;Begin  Tmp = 0; // <- Actually this isn''t even necessary  for X := Low(tmpArray) to High(tmpArray) do begin    for Y := Low(tmpArray[Tmp]) to High(tmpArray[Tmp]) do begin      tmpArray[X,Y] := 0;    end;  end;End;  


But the question I have now is how the heck can I pass an array to a procedure like the one above, without a pre-defined array type (notice TArray would have to have been defined somewhere and the array would have to be of that exact type to be accepted by the procedure). Like I stated before I need to be able to accept and work on any two dimensional array a user might declare. I''m sure there is way to do that, there just has to be!
Advertisement
man you wrote lines over lines but you didn''t think !

type TArray = array of integer;
type T2Array = array of TArray;

et voila a flexible 2d - array.

damn !
Huh? I''m not sure I follow... how will that help me? I''m not worried about having a flexible two dimensional array, I''m worried about writing a procedure that can take in any two dimensional array, no matter how it was defined or declared, and then being able to perform operations on the entire array. The idea is to make a utility procedure that will be used by other individuals programming in Delphi, so I can''t bind my parameter to any pre-defined type, because I have no idea how the programmer plans to define their two dimensional array.

P.S. The High and Low problem was only part of that problem.
I don't think it's possible to create just one procedure for all types. You could just have a couple of procedures, one for integers, one for strings, etc. I see what you want to do, but whatever I tried I got:

quote:Origional quote by my smartarse compiler
Incompatible types: ___ and ___


I'm sure it wouldn't hurt to make a couple of procedures. If you only want one, then you'll probably need to start using pointers or something. Yipee!

~ There's no substitute for failure ~

Edited by - MarkyD on November 11, 2001 11:15:19 AM
Some ideas:

This uses the Windows ZeroMemery function. I thought that it should zero out where the parameter points, but it doesn't work for some reason. Doesn't stop me from posting it, though. Heh, heh.

      procedure ClearArray(ToClear: Pointer);begin     ZeroMemory(ToClear,SizeOf(ToClear));end;begin     ClearArray(@MyArray);end.  


This one kinda works, but only if the parameter passed is of type TIntegerArray, whether it is an array or not. It is a start of how to use pointers, though.

        procedure ClearArray(ToClear: Pointer);type   TIntegerArray = array of array of integer;   PIntegerArray = ^TIntegerArrayvar   TempX,   TempY: integer;   Arr: PIntegerArray;begin     Arr:=PIntegerArray(ToClear);     for TempX:=Low(Arr^) to High(Arr^) do        for TempY:=Low(Arr^[TempX]) to High(Arr^[TempX]) do           Arr^[TempX][TempY]:=0;end;begin     ClearArray(@MyArray);end.      


I've had a few problems running them, though, so I would not be surprised if you find a couple of errors in there.

~ There's no substitute for failure ~

Edited by - MarkyD on November 11, 2001 2:51:06 PM

Edited by - MarkyD on November 11, 2001 2:52:09 PM
Yeah I kept running into the same wall. I tried using Variants and I tried using Pointers, but I just couldn''t seem to pass a two dimensional array without first having a pre-declared array type. I don''t mind much if I need seperate functions for each different element type, but for right now I''m only concerned about a two dimensional array of integers. However that could be declared as:

  VAR  MyArray: array[1..10,1..5] of integer;   


or

  TYPE  TArray = array[1..10,1..5] of integer;VAR  MyArray:  TArray;  


or

  TYPE  PArray = ^TArray;  TArray = array [0..0] of integer;VAR  MyArray: PArray; //with the actual bounds set in main  


or even

  TYPE  TArray = array of integer;VAR  MyArray:  array of TArray;//with the actual bounds set in main  


etc...

I have no way of knowing how the user would have defined their two dimensional array of integers, or what user defined type names they have used. And without forcing the user to use a pre-defined type of my own creation, my procedure should only care that it''s getting a two dimensional array of integers. But how?!?!?!

I just can''t figure it out! If there are any Delphi Guru''s out there please I need asssitance.

This topic is closed to new replies.

Advertisement