procedure TForm1.Timer1Timer(Sender: TObject);
var
  myxm:string;
  f1:textfile;
begin
  timer1.Enabled:=false;
  assignfile(f1,ExtractFilePath(paramstr(0))+'程序.txt');
  reset(f1);
  readln(f1,myxm);
  closefile(f1);  case myxm of
  '执行查询':cx();
  '执行替换':th();
  end;
  
  Application.terminate;
end;上面程序用于判断读入变量的值,决定调用何过程,但delphi提示case myxm of 这一行错误,请问是何原因?该如何修改?

解决方案 »

  1.   

    字符串不能作为case语句的判断条件
      

  2.   

    你可以先存一个数组存上你的条件:  '执行查询' '执行替换' ,读出来的内容与其判断,然后确定一个整形变量的值,通过Case这个整形变量来判断。
      

  3.   

    看来delphi也有好多不如VB的地方
      

  4.   

    String不是有序类型
    怎么CASE?>>看来delphi也有好多不如VB的地方
    每种编译器语法上有自己的处理方式
    如果这个都让你觉得不如VB
    我无话可说
      

  5.   

    在D8中可以,以前的版本都不行。因为,D8用了.net类库,要遵守公共语言规范
      

  6.   

    字符不行的。因为String不是有序类型.......
      

  7.   

    字符串不行的。因为String不是有序类型.......
      

  8.   

    看来delphi也有好多不如VB的地方=========================================呵呵
      

  9.   

    String不是有序类型
    呵呵
    但功能比VB强大的多
      

  10.   

    Delphi中的 CASE 语句限制只能用顺序的(ordinal)类型,以至于不能在其中直接使用字符串,这不能不说是个遗憾。
      
      解决它的根本思想是将字符串列转化成可比较的顺序类型。最简单的方法是将这些字符串作为一个字符串数组,它们在数组中的索引即代表它们各自的顺序。
      
      首先建立 CaseString 函数,用于获取某字符串在一个字符串数组中的顺序:
      
      function CaseString (const s: string; 
                           const x: array of string): Integer; 
      var i: Integer; 
      begin 
      Result:= -1; // Default return parameter 
      for i:= Low (x) to High (x) do begin 
        if s = x[i] then begin  Result:= i;  Exit;  end; 
      end; 
      end; 
      
      Low() 提供第一个数组成员(通常是0),High() 则返回最后一个。因为 CaseString 返回的是待查字符串在字符串数组中的位置,因此,它可以被直接用到 CASE 语句中:
      
      search:= 'delphi3000'; 
      case CaseString (search, ['delphi3000', 
                                 'delphipages', 
                                 'Torry's']) of 
        0: s:= 'Excellent!'; 
        1: s:= 'Good source'; 
        2: s:= 'Not bad!'; 
      end;  
    这里我介绍另一种使用 TStringList 的解决方法。 
      示例1:
      
      var 
        SelectStrings: TStringList; 
      ... 
      
      { Initialization } 
        SelectStrings := TStringList.Create; 
        SelectStrings.Add('First');       (*1*) 
        SelectStrings.Add('Second');      (*2*) 
        SelectStrings.Add('Third');       (*3*) 
      ... 
      
      { Use it } 
      
        case SelectStrings.IndexOf(sPassedString) of  (*4*) 
          0: //First 
            begin 
             <do something> 
            end; 
          1: //Second 
            begin 
             <do something> 
            end; 
          2: //Third 
            begin 
             <do something> 
            end; 
        end; 
      ... 
      { Finalization } 
        SelectStrings.Free; 
      
      上面是大小写敏感的比较,如果要对大小写不敏感则改用下面的比较方法:
      
        SelectStrings.Add(AnsiUpperCase('First'));       (*1*) 
        SelectStrings.Add(AnsiUpperCase('Second'));      (*2*) 
        SelectStrings.Add(AnsiUpperCase('Third'));       (*3*) 
      
        case SelectStrings.IndexOf(AnsiUpperCase(sPassedString)) of  (*4*) 
      
      (注意:不要使用 UpperCase, 应使用 AnsiUpperCase, 否则你的程序将在国际化中工作不正常。) 
      

  11.   

    字符串不能作为case语句的判断条件
    case myxm[1] of
      '执行查询':cx();
      '执行替换':th();
    end;
      

  12.   

    作为case检索的变量只能是顺序类型的,换一个方式。不行就用if呗
      

  13.   

    字符串不能被case判断,用if吧。