.Net Runtime Library for Delphi
Close
Type Conversion

Every value has an associated type, which defines attributes such as the amount of space allocated to the value, the range of possible values it can have, and the members that it makes available. Many values can be expressed as more than one type. For example, the value 4 can be expressed as an integer or a floating-point value. Type conversion creates a value in a new type that is equivalent to the value of an old type, but does not necessarily preserve the identity (or exact value) of the original object.

To perform conversion from derived class to a base class, base class back to the original derived class or Conversion from a type that implements an interface to an interface object that represents that interface, use the wrap method. You can use the conversion types defined in CNClrLib.Host.Helper to perform conversions between inbuilt data types, as well as use TClrConvert class to convert a base data type to another base data type and TClrBitConverter class to Converts base data types to an array of bytes, and an array of bytes to base data types. All the base type conversion types are declared in CNClrLib.Host.Helper namespace.

Conversion between inbuilt Data Types

Widening conversions involve the creation of a new value from the value of an existing type that has either a more restrictive range or a more restricted member list than the target type. Widening conversions cannot result in data loss (although they may result in a loss of precision). Because data cannot be lost.

For example, the _Decimal interface type supports conversions from Byte, Char, Int16, Int32, Int64, SByte, UInt16, UInt32, and UInt64 values using the TClrConvert class defined in the CNClrLib.Host.Helper namespace. The following example illustrates some of these conversions in assigning values to a _Decimal variable.

Delphi
var Console: _Console; byteValue: ClrByte; shortValue: ClrInt16; intValue: ClrInt32; longValue: ClrInt64; ulongValue: ClrUInt64; decimalValue: _Decimal; begin try byteValue := 16; shortValue := -1024; intValue := -1034000; longValue := 1152921504606846976; ulongValue := High(UInt64); Console := CoConsole.CreateInstance; decimalValue := TClrConvert.ToDecimal(byteValue); Console.WriteLine_15('After converting from a Byte value, the Decimal value is {0}.', decimalValue); decimalValue := TClrConvert.ToDecimal(shortValue); Console.WriteLine_15('After converting from a Int16 value, the Decimal value is {0}.', decimalValue); decimalValue := TClrConvert.ToDecimal(intValue); Console.WriteLine_15('After converting from a Int32 value, the Decimal value is {0}.', decimalValue); decimalValue := TClrConvert.ToDecimal(longValue); Console.WriteLine_15('After converting from a Int64 value, the Decimal value is {0}.', decimalValue); decimalValue := TClrConvert.ToDecimal(ulongValue); Console.WriteLine_15('After converting from a UInt64 value, the Decimal value is {0}.', decimalValue); Console.ReadLine; // The example displays the following output: // After assigning a Byte value, the Decimal value is 16. // After assigning a Int16 value, the Decimal value is -1024. // After assigning a Int32 value, the Decimal value is -1034000. // After assigning a value, the Decimal value is 1152921504606846976. // After assigning a Int64 value, the Decimal value is 18446744073709551615. except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end.










































Narrowing conversions involve the creation of a new value from the value of an existing type that has either a greater range or a larger member list than the target type. Because a narrowing conversion can result in a loss of data, it is often require that the conversion be made explicit through a call to a conversion method called wrap method or use the AsXXXX method of the interface object. Example _Decimal.AsIConvertible, the interface _Decimal provides an AsXXXX methods (such as AsIConvertible) which represent all the base classes as well as inherited interfaces defined in the .net framework. Doing so means you don't need to do explicit wrapping conversion, just call the AsXXXX method of the interface and the interface object will be converted to the specific AsXXXX method's return data type. To get the original .net object from the wrap DFL object use the Unwrap method.

Delphi
program ConversionDemo; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNClrLib.Host, CNClrLib.Core, CNClrLib.Core.Intf, CNClrLib.Host.Helper; var AConsole: _Console; ADecimal: _Decimal; AConvertible: _IConvertible; ACultureHelper : _CultureInfoHelper; ACultureInfo : _CultureInfo; AFormatProv:IFormatProvider; AInt32: ClrInt32; begin try AConsole := CoConsole.CreateInstance; //using Wrap Method //========================================================================== //To convert integer value to decimal object, you can use the wrap method of the //_Decimal interface. AInt32 := 45; ADecimal := CoDecimal.Wrap(AInt32); AConsole.WriteLine_15('Convert Integer value ''45'' to Decimal value using the Wrap method, '+ 'the Decimal value is {0}.', ADecimal); //using AsXXXXX methods //========================================================================== //Using C# Char struct inherited from the interface IConvertible, IFormatible //C# Class Declaration Syntax //public struct Char : IComparable, IConvertible, IComparable<char>, IEquatable<char> // //In the Delphi Framework Library Interface _Char, the inherited base classes or interfaces are represented as AsXXXXX //methods, these are: //AsIComparable //AsIConvertible //Convert decimal object to inherited interface IConvertible; AConvertible := ADecimal.AsIConvertible; //Create Static CultureInfo interface type ACultureHelper := CoCultureInfoHelper.CreateInstance; ACultureInfo := ACultureHelper.CurrentCulture; //Convert CultureInfo object to inherited interface IFormatProvider AFormatProv := ACultureInfo.AsIFormatProvider; //Convert char 'c' to sbyte using IConvertible interface AInt32 := AConvertible.ToInt32(AFormatProv); AConsole.WriteLine_17('Convert decimal ''{0}'' to integer value using IConvertible '+ 'interface, the Integer value is {1}.', ADecimal, AInt32); AConsole.ReadKey; // The example displays the following output: // Convert Integer value '45' to Decimal value using the Wrap method, the Decimal value is 45. // Convert decimal '45' to integer value using IConvertible interface, the Integer value is 45. except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end.































































Custom Conversions with the ChangeType Method

In addition to supporting conversions to each of the base types, the TClrConvert class can be used to convert a custom type to one or more predefined types. This conversion is performed by the TClrConvert.ChangeType method, which in turn wraps a call to the IConvertible.ToType method of the value parameter. This means that the object represented by the value parameter must provide an implementation of the IConvertible interface.
 
 
 
 
Widening conversion occurs when a value of one type is converted to another type that is of equal or greater size. A narrowing conversion occurs when a value of one type is converted to a value of another type that is of a smaller size. The tables in this topic illustrate the behaviors exhibited by both types of conversions. For more information see .Net Type Conversion Tables.