Contents |
Implicit Type Conversion
The example below declares two variable, one of type double and the other integer. The double data type is assigned a value and is converted to integer type. When you run the code the result displayed is an integer value.
Public Sub Form_Load() Dim d As Double d = 123.456 Dim i As Integer i = d System.out.println("Integer value is" & i) End Sub
Explicit Type Conversion
When types cannot be implicitly converted you should convert them explicitly. This conversion is also called as cast. Explicit conversions are accomplished using CType function.
Public Sub Form_Load() Dim d As Double d = 123.456 Dim i As Integer i = CInt(d) System.out.println("Integer value is" & i) End Sub
Conversion Options
Option Explicit Statement
Forces explicit declaration of all variables in a file.
Option Strict Statement
Restricts implicit data type conversions to only widening conversions.
Conversion Functions
Type Casting Functions
CBool - use this function to convert to Bool data type CByte - use this function to convert to Byte data type CChar - use this function to convert to Char data type CDate - use this function to convert to Date type CDbl - use this function to convert to Double data type CDec - use this function to convert to Decimal data type CInt - use this function to convert to Integer data type CLng - use this function to convert to Long data type CObj - use this function to convert to Object type CShort - use this function to convert to Short data type CSng - use this function to convert to Single data type CString - use this function to convert to String data type
IsClass Function
This function can be used to test if an object is of a specified type (this function works like the instanceof keyword in Java, or like the "type of"-syntax in Visual Basic).
Public Function IsVBImage(Resource As #IResource) As Boolean If IsClass(Resource, VBImage) Then IsVBImage = True Else IsVBImage = False End If End Function
Cast Function
Public Sub Test() ObjectReferenceAsTargetClass = Cast(ObjectReference, TargetClass) End Sub
