We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Initialising variables once only

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

2
Contributors
1
Reply
19 Hours
Discussion Span
1 Year Ago
Last Updated
2
Views
heatdav
Newbie Poster
1 post since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0679 seconds using 2.67MB