Author: Yoganand Aiyadurai
How to make the same function to return the value you want? How to combine the
co-related functions into one single function and Still get the values what all the
functions returned ?
Answer:
Let us take the example of the functions which returns the Year, Month, Day, Month
name, Day name and the date in a particular format for eg. Britishformat. We have
to write a separate functions for returning the desired values. For eg.
1 function Year(Value: Tdatetime): Word;
2 var
3 vY, vM, vD: Word;
4 begin
5 DecodeDate(now, vY, vM, vD);
6 Result := vY;
7 end;
8
9 function Month(Value: Tdatetime): Word;
10 var
11 vY, vM, vD: Word;
12 begin
13 DecodeDate(now, vY, vM, vD);
14 Result := vM;
15 end;
16
17 function Day(Value: Tdatetime): Word;
18 var
19 vY, vM, vD: Word;
20 begin
21 DecodeDate(now, vY, vM, vD);
22 Result := vD;
23 end;
24
25 function Dayname(Value: Tdatetime): Word;
26 begin
27 Result := Formatdatetime('dddd', now);
28 end;
29
30 function Britishformat(Value: Tdatetime): string;
31 begin
32 Result := Formatdatetime('dd/mm/yyyy', now);
33 end;
Since all these functions are related with date, we can combine them into a single
function and still get all the values by telling the function what value to return.
For this, first of all we have to declare a record constant, under type section of
the unit in which the function is going to reside. Name fields properly as you name
the function and the field value to the desired value that you want to return. For
eg.
34 TMyDate = record
35 Year, Month, Day: Word;
36 ShortMonthName, LongMonthName, ShortDay, LongDay,
37 BritishFormat, AmericanFormat,
38 ItalianFormat, RDBMSFormat: string;
39 LeapYear: Boolean;
40 end;
If you are not worried about the return value, then keep all fieldvalues of the
record as variant. This will reduce the work load of convertion.
Next make a function declaration , depending on the scope of visibility, under the
appropriate section . Lets us name the function as ConvertDate which accepts date
as a Tdatetime value and returns the record of TmyDate.
41
42 function ConvertDate(Value: Tdatetime): TMyDate;
43
44 {Now under the implementation section the function would be as given below.}
45
46 function ConvertDate(Value: Tdatetime): TMyDate;
47 var
48 vY, vM, vD: Word;
49 begin
50 DecodeDate(Value, vY, vM, vD);
51 Result.Year := vY;
52 Result.Month := vM;
53 Result.Day := vD;
54 Result.LeapYear := IsLeapYear(vY);
55 Result.ShortDay := FormatDateTime('ddd', Value);
56 Result.LongDay := FormatDateTime('dddd', Value);
57 Result.ShortMonthName := FormatDateTime('mmm', Value);
58 Result.LongMonthName := FormatDateTime('mmmm', Value);
59 Result.AmericanFormat := FormatDateTime('yyyy/mm/dd', Value);
60 Result.ItalianFormat := FormatDateTime('mm-dd-yyyy', Value);
61 Result.BritishFormat := FormatDateTime('dd/mm/yyyy', Value);
62 Result.RDBMSFormat := FormatDateTime('dd-mmm-yyyy', Value);
63 end;
Calling the function.
If you have three variables varYear, varMonth of word and varBritishformat of
string into which you want to store the return values of the function. Then
varYear := ConvertDate(now).Year;
varMonth := ConvertDate(now).Month;
varBritishformat := ConvertDate(now).BritishFormat;
Combining the functions will reduce the headace of remembering the different function names, reduce the lines of coding, and its easy to use.
|