Link to home
Start Free TrialLog in
Avatar of Fled
Fled

asked on

String to constant

Hello again to all Experts.

In my application I have string constants (very long text) which I store in a files with some other data.  In order to save disk space, I which to save only the name of the constants instead of the resulting strings.  In order to get the information back I would need to have a function which would change the name of the readed string to a constant name.

Here is the pseudo.

const
  gstrMess1 = 'This is the first very long message' ;
  gstrMess2 = 'This is the second very long message' ;
  ...

{String construction}
strAString := <some data> + <some delimiter> + 'gstrMess1' {note that this is a string}

...

Writeln(AFile, strAString) ;

...

{File reading and enterpretation}

Readln(AFile, strAString) ;

...

{Here is the trick}
gstrInfomation := StrToConst('gstrMess1') ;

From a sting representation of the name of a constant, accessing this constant value.

What are your options...

Francois Leduc
Avatar of billious
billious

How about using the registry to store the strings? Documentation on Tregistry and program Regedit should get you started...

...Bill
I think the better way (often used) is an array of constants and constants for index:

const
  MAX_MSG = 2;
  gStrMsg = array[1..MAX_MSG] of string =
  ('This is the first very long message',
   'This is the second very long message');
  MSG_FIRST  = 1;
  MSG_SECOND = 2;

...
gStrMsg[MSG_FIRST]
HI,
There are a sea of solutions:
1. TIniFile (not TRegIniFile);
2. Local DB (for example, Paradox 7);
3. TClientDataSet component with its methods LoadFromFile and SaveToFile.
Sincerely,
Nestorua.
Avatar of Fled

ASKER

Thanks to all.

I cannot use database nor registry in this application.

The thing you demonstrate (Luka) seems to be ok but I still wouldn't be able to save the constant name MSG_FIRST as a sting in a file and read it back to set the variable value.

{File reading and enterpretation}

Readln(AFile, strAString) ;

...

{Here is the trick}
gstrInfomation := StrToConst('MSG_FIRST') ;

I would be force to save the constant index in the array

{String construction}
strAString := <some data> + <some delimiter> + IntToStr(MSG_FIRST) {note that this is a string}

...

Writeln(AFile, strAString) ;

Read it back and interprete it like this:

{File reading and enterpretation}

Readln(AFile, strAString) ;

...

gstrInfomation := gStrMsg[StrToInt(ArrayIndexInTheFile)] ;

This could be OK but I really would like to find a way to transform a tring representation of a constant into a constant like this:

const
  AConst = 'ABC' ;
var
  AVar : string ;

begin
  AVar := StringToConst('AConst') ;
end ;

Thanks

hello Fled, you seem to think that the text you use for constants and variables names (like gstrMess1) are what the compiler uses, they are not used by the compiler, it uses binary references for contansnts and variables, the ascii text is just for us non binary people, so we can read it more easily. instaed of 100100111010001. . .  so if you want a
AVar := StringToConst('AConst') ;
type of function you may need to use some reference other than the text name you use for your constants, ,  I do not think that there is ANY of that const 'text' name left after you compile your program, none of the text charaters are used, they are "converted' to a whatever binary number that suits the compiler. you would just do a series of

if AConst = 'gstrMess1' then Result := gstrMess1;

for all of the Constants


but you may as well use LukA_YJK's method and just use the index number
ASKER CERTIFIED SOLUTION
Avatar of LukA_YJK
LukA_YJK
Flag of Azerbaijan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Fled:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:
       to split points between LukA_YJK, nestorua and Slick812
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Some days and here is the Christmas Time. I wish good luck and good health for you all and for your loved ones

kacor
EE Cleanup Volunteer