Articles   Members Online:
-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
Case statement that *accepts* string values 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
30-Dec-03
Category
Algorithm
Language
Delphi 5.x
Views
230
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Michael Britt

You've probably tried providing a Case statement with string type selector 
expression, to find out that it only takes ordinal types (which string is not). 

The following function enables you to use the Case statement with string type 
variables: 

Answer:

1   function StringToCaseSelect
2     (Selector: string;
3     CaseList: array of string): Integer;
4   var
5     cnt: integer;
6   begin
7     Result := -1;
8     for cnt := 0 to Length(CaseList) - 1 do
9     begin
10      if CompareText(Selector, CaseList[cnt]) = 0 then
11      begin
12        Result := cnt;
13        Break;
14      end;
15    end;
16  end;
17  
18  //Usage:
19  
20  case StringToCaseSelect('Delphi',
21    ['About', 'Borland', 'Delphi']) of
22    0: ShowMessage('You''ve picked About');
23    1: ShowMessage('You''ve picked Borland');
24    2: ShowMessage('You''ve picked Delphi');
25  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