在 case 语句中使用字符串

非常遗憾 Delphi 的 case 语句不支持字符串, 但我觉得这也可能是基于效率的考量;
如果非要在 case 中使用字符串, 也不是不可以变通, 这里提供了五种方法.

本例效果图:

在 case 语句中使用字符串

代码文件:

unit Unit1;



interface



uses

  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 FormCreate(Sender: TObject);

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

    procedure Button3Click(Sender: TObject);

    procedure Button4Click(Sender: TObject);

    procedure Button5Click(Sender: TObject);

    procedure Button6Click(Sender: TObject);

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



uses TypInfo; {操作枚举用}



{初始化一个单选组, 用于测试}

procedure TForm1.FormCreate(Sender: TObject);

begin

  RadioGroup1.Items.CommaText := 'a,bb,ccc,dddd';

  RadioGroup1.ItemIndex := 0;

end;



{这是 case 语句比较常规的用法}

procedure TForm1.Button1Click(Sender: TObject);

begin

  case RadioGroup1.ItemIndex of

    0: Color := clRed;

    1: Color := clYellow;

    2: Color := clLime;

    3: Color := clBlue;

  end;

end;



{方法一: 假如要 case 的字符串的长度不同}

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;

end;



{方法二: 假如要 case 的字符串的第一个字母不同, case 是支持字符的}

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;

end;



{方法三: 借用 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;



{方法四: 借用枚举}

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;

end;



{方法五: 利用对比字符串返回的整数, 这种方法并不太可靠, 但在某种情况下会更灵活}

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;

end;



end.


 
   
窗体文件:

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

  end

end


 
   

你可能感兴趣的:(case)