delphi中的
Data in [1..2] then
这样可以如果我的变量是字符串型的。
例如
String in ['山东','江苏','北京'] 这么编译不过去呢?
应该怎么写呢?
谢谢。

解决方案 »

  1.   

    [1..2]是一个set类型的,set类型的数据必须是有序的
    但是['山东','江苏','北京']不符合set类型的定义...
      

  2.   

    只能是有序类型,或枚举
    不能是字符串的http://lysoft.7u7.net
      

  3.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
    aa:char;
    i:integer;
    begin
      if aa in ['a'..'c'] then begin  end;
      if i in [1..5] then begin  end;
    end;好像只有数字可以,字母可以看作ascII码数字
      

  4.   

    不能用string,这个是delphi的函数,改用别的变量
      

  5.   

    看了这么多,没有一个人能说到重点上,其实很简单,为什么不看看帮助呢?
    谁都知道在这里in是集合操作,
    但是,楼主的问题也就在,什么是集合呢?
    ['山东','江苏','北京'] 是不是集合呢?在线帮助上说:A set is a collection of values of the same ordinal type. 
    所以,这里说明了,集合必须是"ordinal"类型, 也就是序数类型。
    整形是的,浮点型就不是
    枚举是的,因为内部存储其实就是整形。
    布尔型是的,
    字符型也是的,字串就不行,
    其它对象类型也是的。举一而反三,其实这样的情况在Delphi中有很多,case语句也有同样的规定:
    where selectorExpression is any expression of an ordinal type (string types are invalid) and each caseList is one of the following:A numeral, declared constant, or other expression that the compiler can evaluate without executing your program. It must be of an ordinal type compatible with selectorExpression. Thus 7, True, 4 + 5 * 3, 'A', and Integer('A') can all be used as caseLists, but variables and most function calls cannot. (A few built-in functions like Hi and Lo can occur in a caseList. See Constant expressions.)
    A subrange having the form First..Last, where First and Last both satisfy the criterion above and First is less than or equal to Last.A list having the form item1, ..., itemn, where each item satisfies one of the criteria above.再深入些,为什么Delphi要做到这样呢?
    因为这些值是要在编绎期设定的---这个工作由编绎器优化,序数类型就可以在这个时期做了。而对象却不行(必须在运行期才能做 --- 上过编绎原理了吧)