1.11M Members

Initialising variables once only

 
0
 

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

 
0
 

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.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article