2005. január 29., szombat

Combine the co-related functions into one single function


Problem/Question/Abstract:

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.

function Year(Value: Tdatetime): Word;
var
  vY, vM, vD: Word;
begin
  DecodeDate(now, vY, vM, vD);
  Result := vY;
end;

function Month(Value: Tdatetime): Word;
var
  vY, vM, vD: Word;
begin
  DecodeDate(now, vY, vM, vD);
  Result := vM;
end;

function Day(Value: Tdatetime): Word;
var
  vY, vM, vD: Word;
begin
  DecodeDate(now, vY, vM, vD);
  Result := vD;
end;

function Dayname(Value: Tdatetime): Word;
begin
  Result := Formatdatetime('dddd', now);
end;

function Britishformat(Value: Tdatetime): string;
begin
  Result := Formatdatetime('dd/mm/yyyy', now);
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.

TMyDate = record
  Year, Month, Day: Word;
  ShortMonthName, LongMonthName, ShortDay, LongDay,
    BritishFormat, AmericanFormat,
    ItalianFormat, RDBMSFormat: string;
  LeapYear: Boolean;
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.

function ConvertDate(Value: Tdatetime): TMyDate;

Now under the implementation section the function would be as given below.

function ConvertDate(Value: Tdatetime): TMyDate;
var
  vY, vM, vD: Word;
begin
  DecodeDate(Value, vY, vM, vD);
  Result.Year := vY;
  Result.Month := vM;
  Result.Day := vD;
  Result.LeapYear := IsLeapYear(vY);
  Result.ShortDay := FormatDateTime('ddd', Value);
  Result.LongDay := FormatDateTime('dddd', Value);
  Result.ShortMonthName := FormatDateTime('mmm', Value);
  Result.LongMonthName := FormatDateTime('mmmm', Value);
  Result.AmericanFormat := FormatDateTime('yyyy/mm/dd', Value);
  Result.ItalianFormat := FormatDateTime('mm-dd-yyyy', Value);
  Result.BritishFormat := FormatDateTime('dd/mm/yyyy', Value);
  Result.RDBMSFormat := FormatDateTime('dd-mmm-yyyy', Value);
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.

Nincsenek megjegyzések:

Megjegyzés küldése