RAD Studio
|
For cases when the conversion is more complex, you can use a different syntax to specify a function to perform the conversion instead of using a conversion factor. For example, you can't convert temperature values using a conversion factor, because different temperature scales have a different origins.
This example, which comes from the StdConvs unit, shows how to register a conversion type by providing functions to convert to and from the base units.
First, declare variables for the identifiers. The identifiers are used in the cbTemperature conversion family, and the units of measurement are its members:
var
cbTemperature: TConvFamily;
tuCelsius: TConvType;
tuKelvin: TConvType;
tuFahrenheit: TConvType;
TConvFamily cbTemperature; TConvType tuCelsius; TConvType tuKelvin; TConvType tuFahrenheit;
Next, define and register the base unit of the conversion family, which in the example is degrees Celsius. Note that in the case of the base unit, we can use a simple conversion factor, because there is no actual conversion to make:
tuCelsius:=RegisterConversionType(cbTemperature,'Celsius',1);
tuCelsius = RegisterConversionType(cbTemperature,"Celsius",1);
You need to write the code that performs the conversion from each temperature scale to 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; begin Result := ((AValue - 32) * 5) / 9; end; function CelsiusToFahrenheit(const AValue: Double): Double; begin Result := ((AValue * 9) / 5) + 32; end; function KelvinToCelsius(const AValue: Double): Double; begin Result := AValue - 273.15; end; function CelsiusToKelvin(const AValue: Double): Double; begin Result := AValue + 273.15; end;
double __fastcall FahrenheitToCelsius(const double AValue) { return (((AValue - 32) * 5) / 9); } double __fastcall CelsiusToFahrenheit(const double AValue) { return (((AValue * 9) / 5) + 32); } double __fastcall KelvinToCelsius(const double AValue) { return (AValue - 273.15); } double __fastcall CelsiusToKelvin(const double AValue) { return (AValue + 273.15); }
Now that you have the conversion functions, you can register the other measurement units 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);
tuKelvin = RegisterConversionType(cbTemperature, "Kelvin", KelvinToCelsius, CelsiusToKelvin); tuFahrenheit = RegisterConversionType(cbTemperature, "Fahrenheit", FahrenheitToCelsius, CelsiusToFahrenheit);
You can now use the newly registered units to perform conversions in your applications. The global Convert function can convert between any of the conversion types that you registered with the cbTemperature conversion family. For example the following code converts a value from degrees Fahrenheit to degrees Kelvin.
Convert(StrToFloat(Edit1.Text), tuFahrenheit, tuKelvin);
Convert(StrToFloat(Edit1->Text), tuFahrenheit, tuKelvin);
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|