Start New Discussion within our Software Development Community

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

   myBaseArray : myArrayType
   temp_i : integer;

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


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


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


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.

This article has been dead for over six months. Start a new discussion instead.