TypeConversions

The compiler uses relatively strong type checking, which means that an error is generated if you try and assign variables of different types to each other. This is good as it helps prevents program errors through the incorrect assignment of variable types. However, you can typecast a value to override this behavior. For example, you may want to assign a char to a byte or a bit to a boolean, like this

Dim myChar As Char = "A"
Dim myByte As Byte = CByte(myChar)
Dim myBoolean As Boolean = CBool(STATUS.2)

Type assignment is a little more relaxed when assigning floating point to integer or vice versa, so typecasting is not needed. For example,

Sub Main()
   Dim myFloat As Single = 10.3
   Dim myByte As Byte = 0
   myByte = myFloat
   myFloat = myByte
End Sub

Type conversion or typecasting can be particularly useful for controlling code generation when expressions are used. If an expression has more than two operands, temporary storage is automatically allocated by the compiler to store intermediate results. For example,

Dim a As Byte
Dim b As Byte
Dim c As Byte
Dim result As UShort
result = a + b + c

Would translate to something like

 TMP_WORD = a + b
 result = TMP_WORD + c

Notice that the temporary variable is a word size, because adding two bytes may result in a number larger than 8 bits. To override this behavior, you can use a typecast,

result = CByte(a + b) + c

The intermediate storage allocation for (a + b) will now only be a byte. Typecasting can also be used to reduce the size of a declared variable, for example

result = CByte(WordVar) + b + c

This will generate a temporary storage value of 16 bits (rather than 32), because byte + byte will require a 16 bit result. Note the subtle difference between

' MyWord made a byte, added to b, tmp storage = 16 bits
result = CByte(WordVar) + b + c   

' result of (MyWord + b) is byte, tmp storage is 8 bits
result = CByte(WordVar + b) + c 

You do not normally have to worry about mixing types within Firewing expressions, as the compiler will generally promote variables automatically to ensure the correct result is obtained. However, typecasting can be extremely useful for expressions that contain mixed types. For example,

Dim SValue As Integer
Dim UValue As UShort
Dim FValue As Single
Dim Result As UShort
Result = SValue + UValue + FValue

In this example, the compiler needs to ensure that the signed and unsigned integer addition (SValue + UValue) is computed correctly by internally converting and promoting the sub-expression before a signed addition is performed. Next, the intermediate result is converted to a floating point format, before it can be added to FValue. Finally, the floating point intermediate result is converted into an ordinal type and assigned to the variable Result. All these computations need to be done for the correct result to be calculated, given that the compiler knows nothing about the state of the mixed types used.

However, if you understand what your program is doing, you can change the behavior of the compiler expression generator to produce better optimized code. For example, we could write the expression as,

Result = CUShort(SValue) + UValue + CUShort(FValue) 

This would reduce the amount of code needed to support the computation by approximately 70%, when compared against the previous example. Of course, the correct result will only be obtained if the assumptions about the state of SValue and FValue when the expression is computed are correct.

Please remember, the compiler expression generator will produce code to try and obtain the correct results, given any mix of variable types. It does not make assumptions about your code. Changing this behavior may lead to an incorrect result being obtained. Worse, the incorrect result may be intermittent, which makes very difficult debugging sessions. Typecasting is very useful, but it must be used with extreme care.

A table of possible conversion functions can be found here.