Delphi if in 和ord函数问题

ZeaginXie 2019-09-04 04:02:55
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') 确没有问题 请教各位大神这是什么原因?看了网上的代码跟我的一模一样的但却没有问题?
...全文
481 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_22803203 2019-11-08
  • 打赏
  • 举报
回复
都是直接用pos函数来判断a字符串是否在b字符串的,还真没用到过楼主那种方法
  • 打赏
  • 举报
回复
CharInSet内部也是用了in,只是避免了直接使用字符字面值出现的编译器警告
武稀松 2019-10-08
  • 打赏
  • 举报
回复
CharInSet是高版本Delphi提倡的,in的话char常量有WideChar和AnsiChar的歧义
天行归来 2019-10-07
  • 打赏
  • 举报
回复
不科学,D7和D10都可以测试通过,楼主的完整代码是什么? var tmp: string; begin tmp := 'a'; if tmp[1] in ['a', 'b'] then begin showmessage('matched'); end;
秋天之落叶 2019-10-07
  • 打赏
  • 举报
回复
这样也可以
if not(CharInSet(Key, ['0' .. '9', '.', '-', #8])) then
Key := #0;
if (Key = '.') and (Pos('.', Edit4.text) > 0) then
Key := #0;
  • 打赏
  • 举报
回复
小猴子用的是10.3吧,不知道楼主为什么不行~~~
秋天之落叶 2019-09-05
  • 打赏
  • 举报
回复
换到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;
BlueStorm 2019-09-05
  • 打赏
  • 举报
回复
从来没有听说过CharInSet('a', ['a', 'b']) 不行..................
ZeaginXie 2019-09-05
  • 打赏
  • 举报
回复
感谢楼上的回答,都不行,很尴尬,难道是版本的问题?我以前用10.1也是这样,每次都只能用N多个and连接
  • 打赏
  • 举报
回复
但是编译有一个警告
  • 打赏
  • 举报
回复
10.2是可以的,但是便宜有一个警告,这样
if AnsiChar('a') in ['a', 'b'] then ...
就好了
或者可以使用if CharInSet('a', ['a', 'b']) then ...

ZeaginXie 2019-09-04
  • 打赏
  • 举报
回复
关于ord的问题已经通过https://blog.csdn.net/xufeichen/article/details/4480697的方法解决了,但是if的问题还没有

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧