Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 431
Flo Avatar asked Nov 05 '22 09:11

Flo


2 Answers

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;
like image 82
Andreas Rejbrand Avatar answered Nov 15 '22 06:11

Andreas Rejbrand


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

like image 27
Flo Avatar answered Nov 15 '22 05:11

Flo