All Delphi

Everything You Want To Know From VCL, Tips To Snippet All Everything About Delphi And CodeGear Related

Case Statement Using String ..?

29th March 2008

Have you ever wish that delphi case statement can use with string, and you find out that it was can’t and it only received ordinal …? well with a little trick now you can use case with string. Using some function below, now u can use string with case statement.

function StringToCaseSelect(Selector : string;CaseList: array of string):Integer;
var cnt: integer;
begin
   Result:=-1;
   for cnt:=0 to Length(CaseList)-1 do
   begin
      if CompareText(Selector, CaseList[cnt]) = 0 then
      begin
         Result:=cnt;
         Break;
      end;
   end;
end;

Usage:

case StringToCaseSelect('Delphi', ['About','Borland','Delphi']) of
  0:ShowMessage('You''ve picked About') ;
  1:ShowMessage('You''ve picked Borland') ;
  2:ShowMessage('You''ve picked Delphi') ;
end;

Taken From Delphi.About.com

Bookmark and Share
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati

Comments are closed.