Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to find a constant dynamically Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
18-Sep-03
Category
Algorithm
Language
Delphi 2.x
Views
160
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

In my application, the user has a form where he is selecting from a list of 
options. That list of options corresponds to contant values. Is there a way to find 
the value of a constant when all you have is a string containing its name?

Example:

1   const
2     TEST = 5;
3     DELIVERED = 10
4       { ... }
5   
6   sChoice := listboxChoice.Text // -possible values are TEST and DELIVERED
7   iChoice = {missing method}(sChoice)


iChoice would be assigned the value of the constant of the same name as the user's 
selection. No, I can't change the declarations to be enumerated types or anything 
else. They have to be constants. I've seen examples of this sort of thing done for 
Enumerated types, objects and so on using RTTI. But I can't find an example of 
constants, and I can't figure it out.

Answer:

Solve 1:

If enumerations are okay (I don't see how you could use names of constants), try 
this to get mapping of enumeration from string to integer:

8   { ... }
9   type
10    TConstValues = (cvTest, cvDelivered);
11  
12  var
13    values = array[TConstValues] of integer = (5, 10);
14    strings = array[TConstValues] of string = ('TEST', 'DELIVERED');
15  
16    { ... }
17  
18  function GetConstValue(s: string): integer;
19  var
20    t: TConstValues;
21  begin
22    result := -1;
23    for t := low(TConstValues) to high(TConstvalues) do
24      if strings[t] = s then
25      begin
26        result := values[t];
27        break;
28      end;
29  end;



Solve 2:

This is a modification of Solve 1:

30  { ... }
31  const
32    TCVals: array[TConstValues] of integer = (-1, 5, 10);
33    TCStrs: array[TConstValues] of string = ('UNKNOWN', 'TEST', 'DELIVERED');
34    { ... }
35  
36  function GetConstValue(s: string): integer;
37  var
38    t: TConstValues;
39  begin
40    t := high(TConstvalues);
41    while (t > low(TConstValues)) and (CompareText(TCStrs[t], s) <> 0) do
42      dec(t);
43    Result := TCVals[t];
44  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC