Hi. I'm not sure if Delphi can do this, but I'm trying to create a variable that I can create once in the code and can't be changed, after its created. On the surface, it looks like I want a const, but I'm trying to create a variable which is an array that will only have its size defined at run-time.

unit myUnit

INTERFACE
var
   myBaseArray : myArrayType
   temp_i : integer;

type
   myArrayType = array[1..nElements] of double;
   

IMPLEMENTATION

procedure myTestProcedure()
var
   myArray : myArrayType;
   myCounter : integer;
const
   myMaxCounter = 10;
begin
   for myCounter := 1 to myMaxCounter do
   begin
      myArray := myBaseArray;
      // some code to then do some further stuff with myArray
   end;
end;

INITIALIZATION

   for temp_i := 1 to nElements do myBaseArray[temp_i] := 1.0;

end.

Now this is not my actual code, but is illustrative of what I'm trying to do. In this scenario, nElements is read in from a file (i.e. not a const) and I want to make sure that in myTestProcedure, nothing can change myBaseArray.

If anyone knows how I can create this myBaseArray in such a way that nothing can accidentally edit it later (without passing it in as a function argument, as there will be dozens of arrays like this, going into dozens of different procedures and don't want to have to pass hundreds of arguments around all the time!), that would be great

Why not create a class ecapsulating your array. You can do the read from file in the constructor, and allow only read access to the array property.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.