Solved

How to define IfThen functions in one common unit?

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

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 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

Industry Leaders: 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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month8 days, 15 hours left to enroll

617 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