Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
How to combine the co-related functions into one single function Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Delphi 2.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			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 ? 


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;
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;
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;
25  function Dayname(Value: Tdatetime): Word;
26  begin
27    Result := Formatdatetime('dddd', now);
28  end;
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 

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. 
42  function ConvertDate(Value: Tdatetime): TMyDate;
44  {Now under the implementation section the function would be as given below.} 
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. 

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC