Converting a Data Access Data Type to a C++ Type
To manipulate the data in an application, you will need to work with values in the standard C++ types:
const char* IliByte IlvInt IlvFloat IlvDouble IliDecimal IliDate IliBinary IliTable* IlvAny Any
IliValue object can be easily converted into one of the standard C++ types. However, a conversion of this type will only return a meaningful value if the data type of the
IliValue object is compatible with the target C++ standard type.
Type Conversions That Return a Meaningful Value
Data Type | C++ Cast Operator | as<Type>() Method |
---|
IliStringType | const char* | asString(const char* nv1) |
IliStringType IliBooleanType IliByteType IliIntegerType IliFloatType IliDoubleType IliDecimalType | IliByte IlvInt IlvFloat IlvDouble IliDecimal | asBoolean(IlvBoolean nv1) asByte(IliByte nv1) asInteger(IlvInt nv1) asFloat(IlvFloat nv1) asDouble(IlvDouble nv1) asDecimal(const IliDecimal& nv1) |
IliStringType IliDateType IliTimeType | IliDate IliTime | asDate(const IliDate& nv1) asTime(const IliTime& nv1) |
IliBinaryType | IliBinary | asBinary(const IliBinary& nv1) |
Object type Table type | IliTable* | asTable(const IliTable* nvl) |
IliAnyType | IlAny | asAny(IlAny nv1) |
IliValue objects can be converted in one of two ways:
Using a C++ cast operator (implicit or explicit).
By calling one of the
as<Type>() methods available in the
IliValue class.
This is demonstrated in the next example:
IliValue stringValue = “Hello world!”;
const char* str1 = stringValue; // implicit cast
const char* str2 = stringValue.asString(“Null”);
The main difference between these two conversions is that the as<Type> method accepts an extra parameter. In the following example, the asString method returns the string value of the object for which it is called unless the object is null or its data type is not IliStringDataType. In this case, it returns the value of the nv1 parameter.
class IliValue {
public:
operator const char*() const;
const char* asString(const char* nv1 = 0) const;
};
The cast operator returns 0 if the object is null or its type is not IliStringDataType.
IliValue stringValue(IliStringType); //initial value is null
IliValue integerValue = 6;
const char* str1 = stringValue; //str1 == 0
const char* str2 = integerValue; //str2 == 0
const char* str3 = integerValue.asString(“Undefined”); //str3 != 0
You should remember to use the character string returned by the
const char* operator or the
asString() method as soon as possible. If not, the character string can become invalid the next time the
IliValue object is modified.
Numeric data types can be converted to any of the numeric C++ types. However, the conversion can cause a loss of precision and numbers can even be truncated.
IliValue integerValue = 5;
IliValue doubleValue = 9.8;
IlInt anInt = doubleValue; //loss of precision
IlDouble aDouble = integerValue;
The IliStringType data type can also be converted to any of the numeric types as shown in the following example:
IliValue stringValue = “3.14”;
IlInt i = stringValue; // i set to 3
IlDouble d = stringValue; // d set to 3.14
IliValue stringValue = “Not A Number”;
IlInt i = stringValue; // i set to 0
IlDouble d = stringValue; // d set to 0.0
i = stringValue.asInt(-1); // i set to -1
d = stringValue.asDouble(-2.0); // i set to -2.0
An
IliValue object can also be changed using one of the
= operators:
IliValue value = “One”;
value = “Two”;
value = 3; //Data type changed to IliIntegerType
value = 4.0; //Data type changed to IliDoubleType
Published date: 05/24/2022
Last modified date: 02/24/2022