Delphi中如何写这样的语句!? ( 积分: 1 )

  • 主题发起人 主题发起人 lsjkiki
  • 开始时间 开始时间
L

lsjkiki

Unregistered / Unconfirmed
GUEST, unregistred user!
S:=A>B?C:D
如果A>B成立 ,就把运算C结果给S,否则D给S;
 
只能if……else了,你还想怎样?SB
 
if A>B then
S := C
else
S := D;
 
delphi 自带一个奇怪语句可以实现

Conditionally returns one of two specified values.

Unit

MathStrUtilsor

Category

miscellaneous routines

Delphi syntax:

function IfThen(AValue: Boolean; const ATrue: Integer; const AFalse: Integer = 0): Integer; overload;

function IfThen(AValue: Boolean; const ATrue: Int64; const AFalse: Int64 = 0): Int64; overload;
function IfThen(AValue: Boolean; const ATrue: Double; const AFalse: Double = 0.0): Double; overload;
function IfThen(AValue: Boolean; const ATrue: string; const AFalse: string = ''): string;
overload;

C++ syntax:

extern PACKAGE int __fastcall IfThen(bool AValue, const int ATrue const int AFalse);
extern PACKAGE __int64 __fastcall IfThen(bool AValue, const __int64 ATrue const __int64 AFalse);
extern PACKAGE double __fastcall IfThen(bool AValue, const double ATrue const double AFalse);
extern PACKAGE AnsiString __fastcall IfThen(bool AValue, const AnsiString ATrue const AnsiString AFalse);

Description

IfThen checks the expression passed as AValue and returns ATrue if it evaluates to true, or AFalse if it evaluates to false. In Delphi, if the AFalse parameter is omitted, IfThen returns 0 or an empty string when AValue evaluates to False.
 
如果是在select语句里,就用case when 条件句
select case when a>b then c else d end as s from table
 
如白河愁所说,就是用IfThen,不过先Uses Math
这句就是: S:=IfThen(A>B,C,D);

其实Delphi2005之后就有这条语句了,只是很多人不知道而已...
 
至少是 D7 开始有的.
 
接受答案了.
 

Similar threads

回复
0
查看
1K
不得闲
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部