Its actually a blessing that Delphi does not have the same case syntax as
VB.  For what you want to do, VB degrades the select statement to:

if myvariable="Hello" then
else if myvariable="goodbye" then
etc.

This is very inefficient as statistically it takes n/2 string comparisons.
A much better way is to use a hash table, or a binary find on a sorted list
of strings.  With Delphi, the latter comes "out of the box", in the form of
TStringList.  Sort the contents, and do:

var
  idx: Integer;
begin
  idx := 0;
  if TStringList.Find (idx, 0) then begin
    TSomeObject(TStringList.Objects[idx]).DoSomething;
  end;

In theory OO programming never needs a switch statement.

Dennis.


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On
Behalf Of Jeremy Coulter
Sent: Tuesday, 15 February 2000 14:38
To: Multiple recipients of list delphi
Subject: [DUG]: VB Like Case statement


Hi all.

the one thing I DO miss from VB is its case statement which can be a number
or a string.

i.e. select case myvariable
       case "Hello"
           show_hello
      case "goodbye"
           show_goodbye
     end select.

This  has its good and bad points, BUT ofcourse in Delphi it pretty much has
to me an integer etc.
Does anyone have any ideas how this could be implemented in Delphi, OR if
Delphi has a simlilar type of thing....

THanks,

Jeremy Coulter (manager)
Visual Software Solutions
110 Harewood Road
Christchurch
ph +64 3 3521595
fx +64 3 3521596
cell +21 2533214
http://www.vss.co.nz

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to