Automatic and Special Conversions
An automatic conversion lets the compiler change a data type automatically.
No special syntax is necessary.
In the example that follows, ViviFire automatically changes the value of k
to a floating-point value, then assigns that to q
.
Var k As Int32
Var q As Real64
' Int32 can change to the wider Real64.
k = 321
q = k
A special conversion (also known as a type casting or type coercion) uses an operator to change a data type to a different one.
ViviFire supplies two such operators, As
and In
, which change an expression to the necessary data type.
The example below continues from the example before.
The value of q
is changed to Int32
, then assigned to k
.
' q has the value 321.
q = Math.Sqrt(q)
k = q As Int32
' k has the value 18 (rounded square root of 321).
As
operator
Initial type | Can change to |
---|---|
A numeric type1 | All numeric types, String , object2 |
Boolean |
All numeric types, String , object |
Char , array of Char |
String |
An object type | All data types3 |
String |
All numeric types, Char , DateTime , object |
Int8
,UInt8
,Int16
,UInt16
,Int32
,UInt32
,Int64
,UInt64
,Int128
,UInt128
,Real32
,Real64
,Real128
- Must have a type conversion constructor.
- Must have a type conversion method.
In
operator
The operator In
can change a numeric expression to a unit of measure.
If the expression is a unit of measure, it can change it to a different unit from the same base unit.
All units of measure are stored as floating-point types.
Var size = q In metres
' But IN is not necessary if the expression is a literal number.
size = 5.0 cm