Use strings in case statements

Source: Internet
Author: User
Unfortunately, the case statement of Delphi does not support strings, but I think this may also be based on efficiency considerations;
If you want to use a string in case, it is not an alternative. Five methods are provided here.
In this example:


Code file:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls, extctrls; Type tform1 = Class (tform) radiogroup1: tradiogroup; button1: tbutton; button2: tbutton; button3: tbutton; button4: tbutton; button5: tbutton; button6: tbutton; Procedure transaction (Sender: tobject ); procedure extract (Sender: tobject); Procedure button3click (Sender: tobject); Procedure extract (Sender: tobject ); end; var form1: tform1; implementation {$ R *. DFM} uses typinfo; {operation enumeration use} {initialize a single-choice group for testing} procedure tform1.formcreate (Sender: tobject); begin radiogroup1.items. commatext: = 'a, BB, CCC, dddddd'; radiogroup1.itemindex: = 0; end; {This is a common case statement} procedure tform1.button1click (Sender: tobject ); begin case radiogroup1.itemindex of 0: Color: = clred; 1: Color: = clyellow; 2: Color: = cllime; 3: Color: = clblue; end; {Method 1: suppose the length of the string to be case is different} procedure tform1.button2click (Sender: tobject); var STR: string; begin STR: = radiogroup1.items [radiogroup1.itemindex]; case length (STR) of 1: color: = clred; 2: Color: = clyellow; 3: Color: = cllime; 4: Color: = clblue; end; {Method 2: if the first letter of the string to be case is different, case supports characters} procedure tform1.button3click (Sender: tobject); var STR: string; begin STR: = radiogroup1.items [radiogroup1.itemindex]; case STR [1] of 'A': Color: = clred; 'B': Color: = clyellow; 'C': Color: = cllime; 'D': color: = clblue; end; {method 3: Borrow tstringlist} procedure tform1.button4click (Sender: tobject); var list: tstringlist; STR: string; begin list: = tstringlist. create; List. text: = radiogroup1.items. text; STR: = radiogroup1.items [radiogroup1.itemindex]; case list. indexof (STR) of 0: Color: = clred; 1: Color: = clyellow; 2: Color: = cllime; 3: Color: = clblue; end; List. free; end; {Method 4: Borrow enumeration} type tmyenum = (a, BB, CCC, dddd); Procedure tform1.button5click (Sender: tobject); var myenum: tmyenum; STR: string; begin STR: = radiogroup1.items [radiogroup1.itemindex]; myenum: = tmyenum (getenumvalue (typeinfo (tmyenum), STR); Case myenum of a: Color: = clred; BB: color: = clyellow; CCC: Color: = cllime; dddd: Color: = clblue; end; {Method 5: Compare the integers returned by a string. This method is not very reliable, but in some cases it will be more flexible} procedure tform1.button6click (Sender: tobject); var STR: string; begin STR: = radiogroup1.items [radiogroup1.itemindex]; Case comparestr (STR, 'A ') of 0: Color: = clred; 1: Color: = clyellow; 2: Color: = cllime; 3: Color: = clblue; end.
 

Form file:

object Form1: TForm1  Left = 0  Top = 0  Caption = 'Form1'  ClientHeight = 111  ClientWidth = 265  Color = clBtnFace  Font.Charset = DEFAULT_CHARSET  Font.Color = clWindowText  Font.Height = -11  Font.Name = 'Tahoma'  Font.Style = []  OldCreateOrder = False  Position = poDesktopCenter  OnCreate = FormCreate  PixelsPerInch = 96  TextHeight = 13  object Button1: TButton    Left = 16    Top = 15    Width = 75    Height = 25    Caption = 'Button1'    TabOrder = 0    OnClick = Button1Click  end  object Button2: TButton    Left = 97    Top = 15    Width = 75    Height = 25    Caption = 'Button2'    TabOrder = 1    OnClick = Button2Click  end  object Button3: TButton    Left = 16    Top = 46    Width = 75    Height = 25    Caption = 'Button3'    TabOrder = 2    OnClick = Button3Click  end  object RadioGroup1: TRadioGroup    Left = 178    Top = 8    Width = 80    Height = 94    Caption = 'RadioGroup1'    TabOrder = 3  end  object Button4: TButton    Left = 97    Top = 46    Width = 75    Height = 25    Caption = 'Button4'    TabOrder = 4    OnClick = Button4Click  end  object Button5: TButton    Left = 16    Top = 77    Width = 75    Height = 25    Caption = 'Button5'    TabOrder = 5    OnClick = Button5Click  end  object Button6: TButton    Left = 97    Top = 77    Width = 75    Height = 25    Caption = 'Button6'    TabOrder = 6    OnClick = Button6Click  endend
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.