• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 507
  • Last Modified:

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
0
Fled
Asked:
Fled
1 Solution
 
billiousCommented:
How about using the registry to store the strings? Documentation on Tregistry and program Regedit should get you started...

...Bill
0
 
LukA_YJKCommented:
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]
0
 
nestoruaCommented:
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
FledAuthor Commented:
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

0
 
Slick812Commented:
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
0
 
LukA_YJKCommented:
Thanks, Slick812. Fled, I think he is right. Unfortunately there is not some kind of JavaScript eval in Delphi. Meybe there are some third-party tools...
If you really need that conversion you may use the Slick812's method. But maybe there is some hybrid method:
You can store your constant names in a StringList (you can even load it from a plain text file) in order of their numeric values. Then you can use the standard IndexOf to find of the index of the NAME of this constant:

function StrToConst(ConstName: string): string ;
var ConstNo: integer;
begin
  ConstNo := ConstList.IndexOf(ConstName);
  Result := gStrMsg[ConstNo];
end;

Hope it will help...
0
 
CleanupPingCommented:
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.
0
 
kacorretiredCommented:
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now