Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to define IfThen functions in one common unit?

Posted on 2016-10-21
4
Medium Priority
?
173 Views
Last Modified: 2016-10-23
When I want to use IfThen functions I need to use Math and StrUtils units, Math for numeric and StrUtils for string arguments. I also have some of my own IfThen functions that return TVirtualString, some objects and enums, these are defined in CommonUnit.
So, when I wan to use all versions of IfThen functions, I need to use Math, StrUtils and CommonUnit units. It's annoying.

Is it possible to somehow define/link IfThen functions from Math and StrUtils within CommonUnit, so I would only need to use CommonUnit and will have access to all IfThen functions?

Using Delphi Berlin 10.1.
0
Comment
Question by:Delphi_developer
  • 2
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
Ferruccio Accalai earned 2000 total points
ID: 41855393
You can do that by using overload, that means write an overload version of the IfThen function and assign as result the wanted ifthen. To better understand look at this code
unit CommonFunctions;

interface

uses
  Math, StrUtils, SysUtils, classes;
//the 3 IfThen from Math
function IfThen(AValue: Boolean; const ATrue: Integer; const AFalse: Integer = 0): Integer; overload;
function IfThen(AValue: Boolean; const ATrue: Int64; const AFalse: Int64): Int64; overload;
function IfThen(AValue: Boolean; const ATrue: Double; const AFalse: Double = 0.0): Double; overload;
// then the IfThen from StrUtils
function IfThen(AValue: Boolean; const ATrue: string; AFalse: string = ''): string; overload;
//and finally your own ifthen
function IfThen(AValue: Boolean; const ATrue: char; AFalse: char): TStrings; overload;

implementation
//the 3 IfThen from Math
function IfThen(AValue: Boolean; const ATrue: Integer; const AFalse: Integer = 0): Integer;
begin
  result := Math.IfThen(AValue, ATrue, AFalse);
end;

function IfThen(AValue: Boolean; const ATrue: Int64; const AFalse: Int64): Int64;
begin
  result := Math.IfThen(AValue, ATrue, AFalse);
end;

function IfThen(AValue: Boolean; const ATrue: Double; const AFalse: Double): Double;
begin
  result := Math.IfThen(AValue, ATrue, AFalse);
end;

// then the IfThen from StrUtils
Function IfThen(AValue: Boolean; const ATrue: string; AFalse: string = ''): string;
begin
  result := StrUtils.IfThen(AValue, ATrue, AFalse);
end;

//and finally your own ifthen
Function IfThen(AValue: Boolean; const ATrue: char; AFalse: char): TStrings; 
begin
  result.Add(ATrue);
  result.Add(AFalse);
end;

end.

Open in new window

Then you can call any IfThen just by adding CommonFunctions in the uses (no math or strutils necessary now) as follows
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  CommonFunctions;
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  s: TStrings;
begin
  ShowMEssage(IntToStr(IfThen(true, 10, 20))); //from math
  ShowMEssage(IfThen(true, 'True', 'False'));  //from strutils
  s := TStringList.create;
  s.AddStrings(IfThen(false, 'a', 'z'));       //and your own
  ShowMEssage(s[0]);
  ShowMEssage(s[1]);
  s.Free;
end;

end.

Open in new window

1
 

Author Comment

by:Delphi_developer
ID: 41855399
That looks good! So, this means I need to implement every IfThen from Math and StrUtils, is not enough to just define in interface and the implementation would stay in Math and StrUtils units?
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 41855410
No because the call comes from your unit to CommonUnit.
When you overload a function you can create it with different parameters but if you don't write the implentation code you'll get an unsatisfied forward or external declaration

Anyway the IfThen around math and StrUtils are not so much: you don't have to add so many code to your common unit (maybe just copy and paste from mine ;) )
1
 

Author Comment

by:Delphi_developer
ID: 41855418
OK, let me try and set this up.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question