Mega Code Archive

 
Categories / Delphi / Functions
 

How to combine the co related functions into one single function

Title: How to combine the co-related functions into one single function Question: How to make the same function to return the value you want? or 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.