[ Pobierz całość w formacie PDF ]
.Register the conversion familyNext, register the conversion family:cbTemperature := RegisterConversionFamily ( Temperature );Register the base unitNext, define and register the base unit of the conversion family, which in the exampleis degrees Celsius.Note that in the case of the base unit, we can use a simpleconversion factor, because there is no actual conversion to make:tuCelsius:=RegisterConversionType(cbTemperature, Celsius ,1);Write methods to convert to and from the base unitYou need to write the code that performs the conversion from each temperature scaleto and from degrees Celsius, because these do not rely on a simple conversion factor.These functions are taken from the StdConvs unit:function FahrenheitToCelsius(const AValue: Double): Double;beginResult := ((AValue - 32) * 5) / 9;end;function CelsiusToFahrenheit(const AValue: Double): Double;beginResult := ((AValue * 9) / 5) + 32;end;function KelvinToCelsius(const AValue: Double): Double;beginResult := AValue - 273.15;end;5-36 Devel oper s Gui deCo n v e r t i n g me a s u r e me n t sfunction CelsiusToKelvin(const AValue: Double): Double;beginResult := AValue + 273.15;end;Register the other unitsNow that you have the conversion functions, you can register the other measurementunits within the conversion family.You also include a description of the units.The code to register the other units in the family is shown here:tuKelvin := RegisterConversionType(cbTemperature, 'Kelvin', KelvinToCelsius,CelsiusToKelvin);tuFahrenheit := RegisterConversionType(cbTemperature, 'Fahrenheit', FahrenheitToCelsius,CelsiusToFahrenheit);Use the new unitsYou can now use the newly registered units to perform conversions in yourapplications.The global Convert function can convert between any of the conversiontypes that you registered with the cbTemperature conversion family.For example thefollowing code converts a value from degrees Fahrenheit to degrees Kelvin.Convert(StrToFloat(Edit1.Text), tuFahrenheit, tuKelvin);Using a class to manage conversionsYou can always use conversion functions to register a conversion unit.There aretimes, however, when this requires you to create an unnecessarily large number offunctions that all do essentially the same thing.If you can write a set of conversion functions that differ only in the value of aparameter or variable, you can create a class to handle those conversions.Forexample, there is a set standard techniques for converting between the variousEuropean currencies since the introduction of the Euro.Even though the conversionfactors remain constant (unlike the conversion factor between, say, dollars andEuros), you can t use a simple conversion factor approach to properly convertbetween European currencies for two reasons:" The conversion must round to a currency-specific number of digits." The conversion factor approach uses an inverse factor to the one specified by thestandard Euro conversions.However, this can all be handled by the conversion functions such as the following:function FromEuro(const AValue: Double, Factor; FRound: TRoundToRange): Double;beginResult := RoundTo(AValue * Factor, FRound);end;function ToEuro(const AValue: Double, Factor): Double;beginResult := AValue / Factor;end;Us i ng Bas eCLX 5-37Co n v e r t i n g me a s u r e me n t sThe problem is, this approach requires extra parameters on the conversion function,which means you can t simply register the same function with every Europeancurrency.In order to avoid having to write two new conversion functions for everyEuropean currency, you can make use of the same two functions by making them themembers of a class.Creating the conversion classThe class must be a descendant of TConvTypeFactor.TConvTypeFactor defines twomethods, ToCommon and FromCommon, for converting to and from the base units of aconversion family (in this case, to and from Euros) [ Pobierz całość w formacie PDF ]