Show: Object Pascal C++
Display Preferences

Using a Conversion Function

From Appmethod Topics
Jump to: navigation, search

Go Up to Using the RTL Index

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.

Declare variables

First, declare variables for the identifiers. The identifiers are used in the cbTemperature conversion family, and the units of measurement are its members:

Object Pascal:

var
cbTemperature: TConvFamily;
tuCelsius: TConvType;
tuKelvin: TConvType;
tuFahrenheit: TConvType;

C++:

TConvFamily cbTemperature;
TConvType tuCelsius;
TConvType tuKelvin;
TConvType tuFahrenheit;
Note: The units of measurement listed here are a subset of the temperature units actually registered in the StdConvs unit.

Register the conversion family

Next, register the conversion family:

Object Pascal:

cbTemperature := RegisterConversionFamily ('Temperature');

C++:

cbTemperature = RegisterConversionFamily("Temperature");

Register the base unit

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:

Object Pascal:

 tuCelsius:=RegisterConversionType(cbTemperature,'Celsius',1);

C++:

tuCelsius = RegisterConversionType(cbTemperature, "Celsius", 1);

Write methods to convert to and from the base unit

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:

Object Pascal:

 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;

C++:

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);
}

Register the other units

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:

Object Pascal:

tuKelvin := RegisterConversionType(cbTemperature, 'Kelvin', KelvinToCelsius, CelsiusToKelvin);
tuFahrenheit := RegisterConversionType(cbTemperature, 'Fahrenheit', FahrenheitToCelsius, CelsiusToFahrenheit);

C++:

tuKelvin = RegisterConversionType(cbTemperature, "Kelvin", KelvinToCelsius,
		CelsiusToKelvin);
tuFahrenheit = RegisterConversionType(cbTemperature, "Fahrenheit",
		FahrenheitToCelsius, CelsiusToFahrenheit);

Use the new units

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.

Object Pascal:

Convert(StrToFloat(Edit1.Text), tuFahrenheit, tuKelvin);

C++:

Convert(StrToFloat(Edit1->Text), tuFahrenheit, tuKelvin);

See Also

Personal tools
In other languages