?
Solved

String to constant

Posted on 2003-02-21
8
Medium Priority
?
503 Views
Last Modified: 2010-05-18
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
Comment
Question by:Fled
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 7

Expert Comment

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

...Bill
0
 
LVL 3

Expert Comment

by:LukA_YJK
ID: 7998035
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
 
LVL 4

Expert Comment

by:nestorua
ID: 7999475
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
Technology Partners: 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!

 

Author Comment

by:Fled
ID: 7999637
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
 
LVL 34

Expert Comment

by:Slick812
ID: 8006498
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
 
LVL 3

Accepted Solution

by:
LukA_YJK earned 300 total points
ID: 8006612
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
 

Expert Comment

by:CleanupPing
ID: 9317132
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
 
LVL 10

Expert Comment

by:kacor
ID: 9947933
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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question