Author: Tercio Ferdinando Gaudencio Filho
Replacement for the C ternary conditional operator "?"
Answer:
Solve 1:
Original functions by Project JEDI Code Library (JCL).
It's better(read faster) than use Variant Type.
Ps.: Portuguese comments.
1
2 //*******************************************************
3 // Declara??o
4 // Substitui??o do operador "?" em C
5 //*******************************************************
6
7 function Iff(const Condition: Boolean; const TruePart, FalsePart: Boolean): Boolean;
8 overload;
9 function Iff(const Condition: Boolean; const TruePart, FalsePart: Byte): Byte;
10 overload;
11 function Iff(const Condition: Boolean; const TruePart, FalsePart: Cardinal):
12 Cardinal; overload;
13 function Iff(const Condition: Boolean; const TruePart, FalsePart: Char): Char;
14 overload;
15 function Iff(const Condition: Boolean; const TruePart, FalsePart: Extended):
16 Extended; overload;
17 function Iff(const Condition: Boolean; const TruePart, FalsePart: Integer):
18 Integer; overload;
19 function Iff(const Condition: Boolean; const TruePart, FalsePart:
20 Pointer): Pointer; overload;
21 function Iff(const Condition: Boolean; const TruePart, FalsePart:
22 string): string; overload;
23 {$IFDEF SUPPORTS_INT64}
24 function Iff(const Condition: Boolean; const TruePart, FalsePart:
25 Int64): Int64; overload;
26 {$ENDIF SUPPORTS_INT64}
27
28 //*******************************************************
29 // Fun?ões
30 // Substitui??o do operador "?" em C
31 //*******************************************************
32
33 function Iff(const Condition: Boolean; const TruePart, FalsePart:
34 Boolean): Boolean; overload;
35 begin
36 if Condition then
37 Result := TruePart
38 else
39 Result := FalsePart;
40 end;
41 //*******************************************************
42
43 function Iff(const Condition: Boolean; const TruePart, FalsePart:
44 Byte): Byte; overload;
45 begin
46 if Condition then
47 Result := TruePart
48 else
49 Result := FalsePart;
50 end;
51 //*******************************************************
52
53 function Iff(const Condition: Boolean; const TruePart, FalsePart:
54 Cardinal): Cardinal; overload;
55 begin
56 if Condition then
57 Result := TruePart
58 else
59 Result := FalsePart;
60 end;
61 //*******************************************************
62
63 function Iff(const Condition: Boolean; const TruePart, FalsePart:
64 Char): Char; overload;
65 begin
66 if Condition then
67 Result := TruePart
68 else
69 Result := FalsePart;
70 end;
71 //*******************************************************
72
73 function Iff(const Condition: Boolean; const TruePart, FalsePart:
74 Extended): Extended; overload;
75 begin
76 if Condition then
77 Result := TruePart
78 else
79 Result := FalsePart;
80 end;
81 //*******************************************************
82
83 function Iff(const Condition: Boolean; const TruePart, FalsePart:
84 Integer): Integer; overload;
85 begin
86 if Condition then
87 Result := TruePart
88 else
89 Result := FalsePart;
90 end;
91 //*******************************************************
92
93 function Iff(const Condition: Boolean; const TruePart, FalsePart:
94 Pointer): Pointer; overload;
95 begin
96 if Condition then
97 Result := TruePart
98 else
99 Result := FalsePart;
100 end;
101 //*******************************************************
102
103 function Iff(const Condition: Boolean; const TruePart, FalsePart:
104 string): string; overload;
105 begin
106 if Condition then
107 Result := TruePart
108 else
109 Result := FalsePart;
110 end;
111 //*******************************************************
112 {$IFDEF SUPPORTS_INT64}
113
114 function Iff(const Condition: Boolean; const TruePart, FalsePart:
115 Int64): Int64; overload;
116 begin
117 if Condition then
118 Result := TruePart
119 else
120 Result := FalsePart;
121 end;
122 {$ENDIF SUPPORTS_INT64}
Solve 2:
Delphi 6+ has the following functions:
123
124 function IfThen(AValue: Boolean; const ATrue: Integer; const AFalse: Integer = 0):
125 Integer; overload;
126
127 function IfThen(AValue: Boolean; const ATrue: Int64; const AFalse: Int64 = 0):
128 Int64;
129 overload;
130
131 function IfThen(AValue: Boolean; const ATrue: Double; const AFalse: Double = 0.0):
132 Double; overload;
133
134 function IfThen(AValue: Boolean; const ATrue: string; const AFalse: string =
135 ''): string; overload;
|