Flo
Flo

Reputation: 219

Delphi Error E2283 Too many local constants

I am having a compilation problem with my code in Delphi 2006. I am using a static String array:

fsi_names : array [0..FSI_NUM_VARS-1] of string;

In a procedure I call at the start of the program, I assign values to this array. This code is automatically generated by a script that I wrote. It consists of lines similar to the next one:

fsi_names[idFSI_FLIGHT_PATH_ANGLE] := 'FSI_FLIGHT_PATH_ANGLE';

There are overall around 2000 elements to be assigned in this array. I couldn't find out the magic number where the compiler dies, but it works with 1853 and doesn't with 2109.

The thing is that I need this array to convert an ID (which is the index to the array) to a name as a string for various applications.

I know that if I would split the list of assignments and put the parts into different procedures, then it works out. But since the code is auto-generated and changes often, this method is not quite comfortable.

I also thought about putting the contents into a file and read it at runtime, but I would rather keep the number of files I have to ship to a minimum. Also, I would like to protect the contents from the average user, so that he doesn't mess with it.

Do you have an idea how I could either overcome the limitation of the compiler, or change my code to achieve my goal?

Help is very much appreciated.

Upvotes: 5

Views: 1827

Answers (3)

Flo
Flo

Reputation: 219

I FOUND A SOLUTION!

If I initialize my array at the point where I define it, then the compiler does not spit out the error message:

const
  fsi_names : array [0..FSI_NUM_VARS-1] of string = (
    'NAME 0',
    'NAME 1',
    ...
    'LAST NAME'
    );

As far as I can tell, there is no limit regarding the number of string literals if I do it like that.

Thank you so much for your ideas, the one by mj2008 was most helpful!

Have a nice day

Flo

Upvotes: 1

APZ28
APZ28

Reputation: 1007

Alternative is using dynamic array

from
fsi_names : array [0..FSI_NUM_VARS-1] of string;

to
fsi_names: array of string;
SetLength(fsi_names, FSI_NUM_VARS);

Cheers

Upvotes: -2

Andreas Rejbrand
Andreas Rejbrand

Reputation: 108963

If I were you, I would create a simple ASCII text file with the identifiers, so that line idFSI_FLIGHT_PATH_ANGLE + 1 of the file contains the string "FSI_FLIGHT_PATH_ANGLE". Then I would add this file to the application's resources. By doing so, the data will be included in the EXE, and you can easily read the data at run-time:

function GetNthString(const N: integer): string;
var
  RS: TResourceStream;
begin
  RS := TResourceStream.Create(hInstance, 'NAMEOFRESOURCE', RT_RCDATA);
  with TStringList.Create do
    try
      LoadFromStream(RS);
      result := Strings[N];
    finally
      Free;
    end;
  RS.Free;
end;

Upvotes: 3

Related Questions