Solved

How to define IfThen functions in one common unit?

Posted on 2016-10-21
4
78 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 500 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 22

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

734 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