Board index » delphi » Case statement question

Case statement question

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.

 

Re:Case statement question


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

Mark Vaughan

Re:Case statement question


Quote
>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 a crude method:

const
  TheCases = 'OneTwoThreeFour';

  ACase := Pos(AString,TheCases);
  case ACase of
  1:
    ShowMessage('One');
  4:
    ShowMessage('Two');
  7:
    ShowMessage('Three');
  12:
    ShowMessage('Four')
  end;

Dave

Re:Case statement question


Quote
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.

Case statements and a list of 'else if's usually compile into the same
object code. I suggest just using CompareStr with nested else ifs.

If CompareStr(Str1,Str2) = 0 then
  ..
Else If CompareStr(Str1, Str2) = 0 then
  ..
Else If CompareStr(Str1, Str2) = 0 then
  ..
Else
  .. Nothing

Re:Case statement question


In article <4oa7os$...@reznor.larc.nasa.gov>, m.a.vaug...@larc.nasa.gov
says...

Quote

>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' :

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

Re:Case statement question


Suggestion 1.

If there are not to may Cases then it is probably best to use a set
of cascading If statements.

ie.

If Case = 'One' then begin
   {do message One procedure}
   If Case = 'Two' then begin
        {do message two procedure}
      If Case = 'Three' then begin
           {do message three procedure}
           If Case = 'Four' then begin
              {do message four procedure}
         end else begin
            {error procedure}
         end;
        end;
   end;
end;

This does not work quite as fast a a case statement, however for a
limited set strings the performance difference if minimal.

Suggestion 2.

If you place your list of cases in a TString list object (i.e. the
list of values available in a combo box). then you can use the
ItemIndex property of the control a runtim, which is an integer.

Any problems, mail me.

Edwin Harrell

Other Threads