Solved

How to define IfThen functions in one common unit?

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now