Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[Tips] Immediate If (IIF) implementation

delphi

  • Please log in to reply
No replies to this topic

#1 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 02 January 2011 - 11:31 AM

There is a post in one mailing list asking why there is no immediate if (IIF) in Delphi. Although it is a fact that there is no built-in IIF in Delphi, but it's actually too easy to implement. So you can do it by yourself.

Here are some implementation of IIF of mine. Note that the variant version is a little slower than the others, although not very significant in most cases.

{in interface section of the unit, to make the routines available globally}
interface

  function IIF(const ACondition: Boolean; const ATrueResult, AFalseResult: Integer): Integer; overload;
  function IIF(const ACondition: Boolean; const ATrueResult, AFalseResult: Double): Double; overload;
  function IIF(const ACondition: Boolean; const ATrueResult, AFalseResult: string): string; overload;
  function IIF(const ACondition: Boolean; const ATrueResult, AFalseResult: variant): variant; overload;

{in implementation section of the unit}
implementation

function IIF(const ACondition: Boolean; const ATrueResult, AFalseResult: Integer): Integer;
begin
  if ACondition then
    Result := ATrueResult
  else
    Result := AFalseResult;
end;

function IIF(const ACondition: Boolean; const ATrueResult, AFalseResult: Double): Double;
begin
  if ACondition then
    Result := ATrueResult
  else
    Result := AFalseResult;
end;

function IIF(const ACondition: Boolean; const ATrueResult, AFalseResult: string): string;
begin
  if ACondition then
    Result := ATrueResult
  else
    Result := AFalseResult;
end;

function IIF(const ACondition: Boolean; const ATrueResult, AFalseResult: variant): variant;
begin
  if ACondition then
    Result := ATrueResult
  else
    Result := AFalseResult;
end;

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download