How to define IfThen functions in one common unit?
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?
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
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;interfaceuses Math, StrUtils, SysUtils, classes;//the 3 IfThen from Mathfunction 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 StrUtilsfunction IfThen(AValue: Boolean; const ATrue: string; AFalse: string = ''): string; overload;//and finally your own ifthenfunction IfThen(AValue: Boolean; const ATrue: char; AFalse: char): TStrings; overload;implementation//the 3 IfThen from Mathfunction 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 StrUtilsFunction IfThen(AValue: Boolean; const ATrue: string; AFalse: string = ''): string;begin result := StrUtils.IfThen(AValue, ATrue, AFalse);end;//and finally your own ifthenFunction IfThen(AValue: Boolean; const ATrue: char; AFalse: char): TStrings; begin result.Add(ATrue); result.Add(AFalse);end;end.
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?
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
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
Delphi_developerAuthor Commented:
OK, let me try and set this up.
0
Featured Post
Be seen. Boost your questionâ€™s priority for more expert views and faster solutions
Open in new window
Then you can call any IfThen just by adding CommonFunctions in the uses (no math or strutils necessary now) as followsOpen in new window