Delphi 10.3 使用if多条件例如 if 'a' in ['a', 'b'] 时报错,提示operator not applicable to this operand type;
想将字符串转A码使用
ord(tmp)   //tmp:string='a' 
时也报错,提示incompatible type
但是直接ord('a') 确没有问题
请教各位大神这是什么原因?看了网上的代码跟我的一模一样的但却没有问题?

解决方案 »

  1.   

    关于ord的问题已经通过https://blog.csdn.net/xufeichen/article/details/4480697的方法解决了,但是if的问题还没有
      

  2.   

    10.2是可以的,但是便宜有一个警告,这样
    if AnsiChar('a') in ['a', 'b'] then ...
    就好了
    或者可以使用if CharInSet('a',  ['a', 'b']) then ...
      

  3.   

    感谢楼上的回答,都不行,很尴尬,难道是版本的问题?我以前用10.1也是这样,每次都只能用N多个and连接
      

  4.   

    从来没有听说过CharInSet('a',  ['a', 'b']) 不行..................
      

  5.   

    换到KeyPress事件试一试:procedure TFormMain.DBEditEh4KeyPress(Sender: TObject; var Key: Char);
    var
      i: Integer;
    begin
      if DMADO.ADO考试.IsEmpty then
        Exit;  if (AnsiChar(Key) in [#13, Char(VK_DOWN)]) and (ActiveControl is TDBEditEh) then
      begin
        Key := #0;
      

  6.   

    这样也可以
    if not(CharInSet(Key, ['0' .. '9', '.', '-', #8])) then
        Key := #0;
      if (Key = '.') and (Pos('.', Edit4.text) > 0) then
        Key := #0;
      

  7.   

    不科学,D7和D10都可以测试通过,楼主的完整代码是什么?var
      tmp: string;
    begin
      tmp := 'a';
      if tmp[1] in ['a', 'b'] then
      begin
        showmessage('matched');
      end;
      

  8.   

    CharInSet是高版本Delphi提倡的,in的话char常量有WideChar和AnsiChar的歧义
      

  9.   

    CharInSet内部也是用了in,只是避免了直接使用字符字面值出现的编译器警告
      

  10.   

    都是直接用pos函数来判断a字符串是否在b字符串的,还真没用到过楼主那种方法