Re:Case statement question
Quote
b...@aa.net (Bob Richardson) wrote:
>In article <4oa7os$...@reznor.larc.nasa.gov>, m.a.vaug...@larc.nasa.gov
>says...
>>In article <4oa5jo$...@news-f.iadfw.net>,
>> mgma...@iadfw.net (Michael Mahagan) wrote:
>>]-I have a varalible of type string. I want to use this variable as a
>>]-case selector, ie. based on the string I want to go to a certian case
>>]-statement. How do I do this? I know strings are invalid case
>>]-selectors. Thanks in advance.
>>]-
>>here's one way...
>>Write a function that returns a unique ordinal value for each
>>string that your case statement needs to operate on...
>> Function StringValue(const S : string) : integer;
>>Then write your case statement using that function...
>> CASE StringValue(MyString) of
>A similar, but less flexible approach, is to convert your string to
>type CHAR, which is especially easy if the first letter is unique.
>var c : char;
>begin
> C := mystring[1];
> case C of
> 'A' :
A quick way to get a number from a string is to use the Pos function
to look up your string's position within a concatenated delimited
string of the possibiltites. E.g., (OTTOMH, not tested)
case Pos('<'+mystring+'>','<first> ' +
'<second>' +
'<third> ') of
0: ShowMessage('not found');
1+8*0: ShowMessage('FIRST!');
1+8*1: ShowMessage('SECOND!');
1+8*2: ShowMessage('THIRD!');
end;
I like to pad with blanks to position at even intervals, just to
make it easier to figure the case numbers, but it's not mandatory
(although that makes it easy to compute a useful index for arrays
etc). Nor are the delimiters mandatory, if the match possibilities
are unambiguous.
HTH,
Regards,
Bengt Richter