Solved

How to define IfThen functions in one common unit?

Posted on 2016-10-21
4
59 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Delphi - replicating a form 8 81
Mobile Keyboard covers the display of the TMemo 3 96
Multi-layered image in FireMonkey 9 48
Twebbrowser add css to the header 3 27
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 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